1

JSON响应:

{
    "categories": [{
            "id": "1",
            "category": "category1"
        },
        {
            "id": "2",
            "category": "category2"
        }
    ]
}

JSON 泽西服务:

@GET
@Produces("application/json")
public List<Categories> GetAllCategories() {
    return CategoriesDAO.getInstance().getAll();
}

我使用 EJB 注释进行休眠映射,使用 jaxB 进行反序列化和序列化 JSON。

模型:

@Entity
@XmlRootElement
public class Categories {
    /** @pdOid f9032734-7d05-4867-8275-bf10813c3748 */
    @Id
    @GeneratedValue
    private Integer id;
    private String category;

    public Categories() {
        // TODO Add your own initialization code here.
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer newId) {
        this.id = newId;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String newCategory) {
        this.category = newCategory;
    }
} 

泽西客户端代码:

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource r = client.resource("http://tomcat.com/GetAllCategories");
List<Categories> output = r.get(new GenericType<List<Categories>>() {});
 

例外:

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@cc4364; line: 1, column: 1]
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:564)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:524)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:696)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:196)
    at com.fit.test.Json.GetAllKategorijeJson.main(GetAllKategorijeJson.java:34)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@cc4364; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308)
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
    at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:554)
    ... 4 more
4

1 回答 1

1

Brate,slusaj ......我有同样的问题。我正在构建一个简单的 Restful 服务,当 GET 方法执行得很好时,JAXB 从我的带有 JaxB 注释的 POJO 类返回 JSON。当我的方法是@Consuming JSON(POST、PUT 案例)时,我发现 JAXB 存在一些问题。

在构建 Jersey / JSON 服务时,最好使用 JACKSON 而不是 JaxB 注释(你会发现很多关于它的谷歌搜索)。问题是 JAXB 在从 JSON 到 POJO 对象的转换(反序列化)方面做得不好。带有 @XMLRootElement 的 JAXB 非常适合使用 XML,但不适用于 JSON。

这就是我在 web.xml 中启用 Jackson 对 JSON 的反序列化/序列化的内容:

<!--  JAXB works great with XML but with JSON it's much better to use Jackson. Jersey will use Jackson -->
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

Google for .jar 文件需要包含在 Jersey 中才能使用 Jackson……祝你好运……

于 2012-12-15T20:11:51.213 回答