2012-11-21 31 views
5

此代码工作正常,如果我传递一个类(MyClass的)已经@XmlRoolElement如何传输的原始列表与新泽西+ JAXB + JSON

客户

WebResource webResource = restClient.resource(getRessourceURL()); 
return webResource.get(new GenericType<List<MyClass>>(){}); 

但如果我尝试转移原始,比如字符串,整数,布尔等..

客户

WebResource webResource = restClient.resource(getRessourceURL()); 
return webResource.get(new GenericType<List<Integer>>(){}); 

我收到错误:

不能编组型“java.lang.Integer中的”为元素,因为它缺少一个@XmlRootElement注释

我得到完全相同的结果发送实体参数,以我的请求时:

客户

WebResource webResource = restClient.resource(getRessourceURL()); 
return webResource.post(new GenericType<List<Integer>>(){}, Arrays.toList("1")); 

服务器

@GET 
@Path("/PATH") 
@Produces(MediaType.APPLICATION_JSON) 
public List<MyClass> getListOfMyClass(List<Integer> myClassIdList) 
{ 
    return getMyClassList(myClassIdList); 
} 

有没有办法来转院这种名单没有为这些基本类型创建一个包装类?还是我错过了明显的东西?

回答

1

我发现了一个解决方法,通过手工控制un// marshalling,没有泽西岛。

客户

WebResource webResource = restClient.resource(getRessourceURL()); 
return webResource.post(new GenericType<List<Integer>>(){}, JAXBListPrimitiveUtils.listToJSONArray(Arrays.toList("1"))); 

服务器

@GET 
@Path("/PATH") 
@Produces(MediaType.APPLICATION_JSON) 
public List<MyClass> getListOfMyClass(JSONArray myClassIdList) 
{ 
    return getMyClassList(JAXBListPrimitiveUtils.<Integer>JSONArrayToList(myClassIdList)); 
} 

而且UTIL类我用:

import java.util.ArrayList; 
import java.util.List; 

import org.codehaus.jettison.json.JSONArray; 
import org.codehaus.jettison.json.JSONException; 

public class JAXBListPrimitiveUtils 
{ 

    @SuppressWarnings("unchecked") 
    public static <T> List<T> JSONArrayToList(JSONArray array) 
    { 
    List<T> list = new ArrayList<T>(); 
    try 
    { 
     for (int i = 0; i < array.length(); i++) 
     { 
     list.add((T)array.get(i)); 
     } 
    } 
    catch (JSONException e) 
    { 
     java.util.logging.Logger.getLogger(JAXBListPrimitiveUtils.class.getName()).warning("JAXBListPrimitiveUtils :Problem while converting JSONArray to arrayList" + e.toString()); 
    } 

    return list; 
    } 

    @SuppressWarnings("rawtypes") 
    public static JSONArray listToJSONArray(List list) 
    { 
    return new JSONArray(list); 
    } 
}