Java 如何使用Jersey+;JAXB+;JSON

Java 如何使用Jersey+;JAXB+;JSON,java,json,list,jaxb,jersey,Java,Json,List,Jaxb,Jersey,如果我传输一个具有@XmlRoolement的类(MyClass),那么这段代码可以正常工作 客户端 WebResource webResource = restClient.resource(getRessourceURL()); return webResource.get( new GenericType<List<MyClass>>(){} ); WebResource webResource = restClient.resource(getRessource

如果我传输一个具有@XmlRoolement的类(MyClass),那么这段代码可以正常工作

客户端

WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<MyClass>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<Integer>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, Arrays.toList("1"));
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, JAXBListPrimitiveUtils.listToJSONArray( Arrays.toList("1") ));
WebResource-WebResource=restClient.resource(getRessourceURL());
返回webResource.get(新的GenericType(){});
但是如果我尝试传输一个基本元素,比如字符串、整数、布尔值等等

客户端

WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<MyClass>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<Integer>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, Arrays.toList("1"));
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, JAXBListPrimitiveUtils.listToJSONArray( Arrays.toList("1") ));
WebResource-WebResource=restClient.resource(getRessourceURL());
返回webResource.get(新的GenericType(){});
我得到一个错误:

无法将类型“java.lang.Integer”封送为元素,因为它缺少@XmlRootElement注释

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

客户端

WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<MyClass>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<Integer>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, Arrays.toList("1"));
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, JAXBListPrimitiveUtils.listToJSONArray( Arrays.toList("1") ));
WebResource-WebResource=restClient.resource(getRessourceURL());
返回webResource.post(新的GenericType(){},Arrays.toList(“1”));
服务器

@GET
@Path("/PATH")
@Produces(MediaType.APPLICATION_JSON)
public List<MyClass> getListOfMyClass( List<Integer> myClassIdList)
{
  return getMyClassList(myClassIdList);
}
@GET
@Path("/PATH")
@Produces(MediaType.APPLICATION_JSON)
public List<MyClass> getListOfMyClass(JSONArray myClassIdList)
{
  return getMyClassList(JAXBListPrimitiveUtils.<Integer>JSONArrayToList(myClassIdList) );
}
@GET
@路径(“/Path”)
@产生(MediaType.APPLICATION_JSON)
公共列表getListOfMyClass(列表MyClassId列表)
{
返回getMyClassList(MyClassId列表);
}

有没有一种方法可以在不为这些基本类型创建包装器类的情况下传输此类列表??还是我遗漏了一些明显的东西?

我找到了一个解决办法,不用球衣,用手控制联合国/编组

客户端

WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<MyClass>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.get( new GenericType<List<Integer>>(){} );
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, Arrays.toList("1"));
WebResource webResource = restClient.resource(getRessourceURL());
return webResource.post( new GenericType<List<Integer>>(){}, JAXBListPrimitiveUtils.listToJSONArray( Arrays.toList("1") ));
WebResource-WebResource=restClient.resource(getRessourceURL());
返回webResource.post(新的GenericType(){},JAXBListPrimitiveUtils.listToJSONArray(Arrays.toList(“1”));
服务器

@GET
@Path("/PATH")
@Produces(MediaType.APPLICATION_JSON)
public List<MyClass> getListOfMyClass( List<Integer> myClassIdList)
{
  return getMyClassList(myClassIdList);
}
@GET
@Path("/PATH")
@Produces(MediaType.APPLICATION_JSON)
public List<MyClass> getListOfMyClass(JSONArray myClassIdList)
{
  return getMyClassList(JAXBListPrimitiveUtils.<Integer>JSONArrayToList(myClassIdList) );
}
@GET
@路径(“/Path”)
@产生(MediaType.APPLICATION_JSON)
公共列表getListOfMyClass(JSONArray myClassIdList)
{
返回getMyClassList(JAXBListPrimitiveUtils.JSONArrayToList(MyClassId列表));
}
我使用的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);
  }
}
import java.util.ArrayList;
导入java.util.List;
导入org.codehaus.jettison.json.JSONArray;
导入org.codehaus.jettison.json.JSONException;
公共类JAXBListPrimitiveUtils
{
@抑制警告(“未选中”)
公共静态列表JSONArrayToList(JSONArray数组)
{
列表=新的ArrayList();
尝试
{
对于(int i=0;i