無法反序列化的java.util.ArrayList實例出來VALUE_STRING的 [英] Can not deserialize instance of java.util.ArrayList out of VALUE_STRING

查看:41007
本文介紹了無法反序列化的java.util.ArrayList實例出來VALUE_STRING的的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有澤西構建和部署在AppEngine上REST服務。其余的服務實現了動詞PUT消耗的應用程序/ json的媒體類型。數據綁定是由杰克遜進行的。

動詞消耗了企業部門的關系再在JSON psented $ P $為

  {名:MYENTERPRISE,部門:HR,IT,SC]}

在客戶端,我用GSON到JSON重新presentation轉換為Java對象。然后,我的對象傳遞給我的REST服務,它工作正常。

問題:

當我重新JSON presentation具有集合中只有一個項目

  {名:MYENTERPRISE,部門:HR]}

該服務無法反序列化對象。

 注意:/企業/企業:組織codehaus.jackson.map.JsonMappingException:
在無法反序列化的java.util.ArrayList實例出來VALUE_STRING令牌
[來源:org.mortbay.jetty.HttpParser$Input@5a9c5842;行:1,列:2

據報道由其他用戶,解決的辦法是添加標志ACCEPT_SINGLE_VALUE_AS_ARRAY(例如,<一href=\"http://stackoverflow.com/questions/14319573/jersey-can-not-deserialize-instance-of-arraylist-out-of-string\">Jersey:無法反序列化ArrayList的實例,從字符串的)。然而,由于在服務方面,它都將透明的杰克遜做我不是一個控制ObjectMapper。

問:

有沒有在服務端配置ObjectMapper使ACCEPT_SINGLE_VALUE_AS_ARRAY的方法嗎?注解? web.xml中?

code詳情

Java對象:

  @XmlRootElement
公共類企業{
????私人字符串名稱;
????私人列表&LT;串GT;部門;????公共企業(){}????公共字符串的getName(){
????????返回名稱;
????}
????公共無效setname可以(字符串名稱){
????????this.name =名稱;
????}
????公開名單&LT;串GT; getDepartments(){
????????返回部門;
????}
????公共無效setDepartments(列表&LT;串GT;部門){
????????this.departments =部門;
????}
}

REST服務端:

  @PUT
????@Consumes(MediaType.APPLICATION_JSON)
????@Produces(MediaType.APPLICATION_JSON)
????@Path(/企業)
????市民反應putEnterprise(企業企業,
????????????@Context HttpServletRequest的REQ){
?????????...
????}

客戶端:

  ...
字符串jsonString ={\\名稱\\:\\MYENTERPRISE \\,\\部門\\:[\\HR \\]};
企業企業= gson.fromJson(jsonString,Enterprise.class);
的System.out.println(gson.toJson(企業));
響應= webResource
???????????.TYPE(MediaType.APPLICATION_JSON)
???????????。把(ClientResponse.class,企業);
如果(response.getStatus()&GT; = 400){
????????拋出新的RuntimeException(失?。篐TTP錯誤code:+ response.getStatus());
}
...


解決方案

這是我的老問題解決方案:

我實現我自己的ContextResolver為了使DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY功能。

 包org.lig.hadas.services.mapper;進口javax.ws.rs.Produces;
進口javax.ws.rs.core.MediaType;
進口javax.ws.rs.ext.ContextResolver;
進口javax.ws.rs.ext.Provider;。進口組織codehaus.jackson.map.DeserializationConfig;
。進口組織codehaus.jackson.map.ObjectMapper;@Produces(MediaType.APPLICATION_JSON)
@Provider
公共類ObjectMapperProvider實現ContextResolver&LT; ObjectMapper&GT;
{
???ObjectMapper映射;???公共ObjectMapperProvider(){
???????映射器=新ObjectMapper();
???????mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,真);
???}
???@覆蓋
???公共ObjectMapper的getContext(類&LT;&GT;類型){
???????返回映射器;
???}
}

和在web.xml我注冊了我的包入servlet定義...

 &LT;&servlet的GT;
????&LT; servlet的名稱&gt; ...&LT; / servlet的名稱&gt;
????&LT;的servlet類&GT; com.sun.jersey.spi.container.servlet.ServletContainer&LT; / servlet的類&GT;
????&LT;初始化參數&GT;
????????&LT;參數-名稱&gt;&com.sun.jersey.config.property.packages LT; /參數 - 名稱&gt;
????????&LT;參數值&GT; ...; org.lig.hadas.services.mapper&LT; /參數值&GT;
????&LT; /初始化參數&GT;
????...
&LT; / servlet的&GT;

...其余全部是透明球衣/杰克遜完成。

I have a REST service built with Jersey and deployed in the AppEngine. The REST service implements the verb PUT that consumes an application/json media type. The data binding is performed by Jackson.

The verb consumes an enterprise-departments relation represented in JSON as

{"name":"myEnterprise", "departments":["HR","IT","SC"]}

In the client side, I use gson to convert the JSON representation into a java object. Then, I pass the object to my REST service and it works fine.

Problem:

When my JSON representation has only one item in the collection

{"name":"myEnterprise", "departments":["HR"]}

the service cannot deserialize the object.

ATTENTION: /enterprise/enterprise: org.codehaus.jackson.map.JsonMappingException: 
Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at 
[Source: org.mortbay.jetty.HttpParser$Input@5a9c5842; line: 1, column: 2

As reported by other users, the solution is to add the flag ACCEPT_SINGLE_VALUE_AS_ARRAY (e.g., Jersey: Can not deserialize instance of ArrayList out of String). Nevertheless, I am not controlling an ObjectMapper because in the service side it is transparently made by Jackson.

Question:

Is there a way to configure the ObjectMapper on the service side to enable ACCEPT_SINGLE_VALUE_AS_ARRAY? annotations? web.xml?

Code details

Java object:

@XmlRootElement
public class Enterprise {
    private String name;
    private List<String> departments;

    public Enterprise() {}

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<String> getDepartments() {
        return departments;
    }
    public void setDepartments(List<String> departments) {
        this.departments = departments;
    }
}

The REST service side:

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/enterprise") 
    public Response putEnterprise(Enterprise enterprise,
            @Context HttpServletRequest req){
         ...
    }

Client side:

...
String jsonString = "{\"name\":\"myEnterprise\", \"departments\":[\"HR\"]}";
Enterprise enterprise = gson.fromJson(jsonString, Enterprise.class);
System.out.println(gson.toJson(enterprise));
response = webResource              
           .type(MediaType.APPLICATION_JSON)
           .put(ClientResponse.class,enterprise);
if (response.getStatus() >= 400) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
...

解決方案

This is the solution for my old question:

I implemented my own ContextResolver in order to enable the DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY feature.

package org.lig.hadas.services.mapper;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

@Produces(MediaType.APPLICATION_JSON)
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
   ObjectMapper mapper;

   public ObjectMapperProvider(){
       mapper = new ObjectMapper();
       mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
   }
   @Override
   public ObjectMapper getContext(Class<?> type) {
       return mapper;
   }
}

And in the web.xml I registered my package into the servlet definition...

<servlet>
    <servlet-name>...</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>...;org.lig.hadas.services.mapper</param-value>        
    </init-param>
    ...
</servlet>

... all the rest is transparently done by jersey/jackson.

這篇關于無法反序列化的java.util.ArrayList實例出來VALUE_STRING的的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
相關文章
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放