java中的restfulweb服务

java中的restfulweb服务,java,xml,rest,post,Java,Xml,Rest,Post,我试图将资源中的Long列表作为post数据传递,消费类型为application/xml。我还传递了两个路径参数。它给了我一个例外“不支持媒体类型” 请帮我解决这个问题。 这是代码,我有个例外 @POST @Path("/temp/{abc}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public List<Long> createUser2

我试图将资源中的
Long
列表作为post数据传递,消费类型为
application/xml
。我还传递了两个路径参数。它给了我一个例外“不支持媒体类型”

请帮我解决这个问题。 这是代码,我有个例外

@POST
    @Path("/temp/{abc}")
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    public List<Long> createUser2(List<User> users,@PathParam("abc") String abc) {
//.................//
        List<Long> listLong=new ArrayList<Long>();
        listLong.add(1L);
        listLong.add(2L);
        System.out.println("temp called");
        return listLong;
    }




> org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
> MessageBodyWriter not found for media type=application/xml
@POST
@路径(“/temp/{abc}”)
@使用(MediaType.APPLICATION_XML)
@生成(MediaType.APPLICATION\u XML)
公共列表createUser2(列表用户,@PathParam(“abc”)字符串abc){
//.................//
List listLong=new ArrayList();
添加(1L);
listLong.add(2L);
系统输出打印项次(“临时调用”);
返回列表长;
}
>org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
>找不到媒体类型为application/xml的MessageBodyWriter

问题在于,没有转换代码知道如何将
长的
列表
自动更改为XML。至少,关于包含元素的名称的信息必须存在,而JAXB(默认支持的机制)仅在类级别应用此类信息

修复方法是创建一个带有适当JAXB注释的包装器类并返回该类。您可能需要调整类以获得所需的序列化,但这并不困难

@XmlRootElement(name = "userinfo")
public class UserInfo {
    @XmlElement
    public List<Long> values;
    // JAXB really requires a no-argument constructor...
    public UserInfo() {}
    // Convenience constructor to make the code cleaner...
    public UserInfo(List<Long> theList) {
        values = theList;
    }
}
@XmlRootElement(name=“userinfo”)
公共类用户信息{
@XmlElement
公共列表值;
//JAXB确实需要一个无参数构造函数。。。
公共用户信息(){}
//方便构造函数使代码更干净。。。
公共用户信息(列表){
值=列表;
}
}
@POST
@路径(“/temp/{abc}”)
@使用(MediaType.APPLICATION_XML)
@生成(MediaType.APPLICATION\u XML)
//注意结果类型的变化
公共用户信息createUser2(列出用户,@PathParam(“abc”)字符串abc){
//.................//
List listLong=new ArrayList();
添加(1L);
listLong.add(2L);
系统输出打印项次(“临时调用”);

return new UserInfo(listLong);//您能给我们看一些将数据发送到API的代码吗?我们不是无所不知的。@POST@Path(“/temp/{abc}”)@Consumes(MediaType.APPLICATION_XML)@products(MediaType.APPLICATION_XML)public List createUser2(List users,@PathParam(“abc”)String abc){/../List listLong=new ArrayList();listLong.add(1L);listLong.add(2L);System.out.println(“临时调用”);返回listLong;}这是代码,我得到了一个异常org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:MessageBodyWriter找不到媒体类型=application/xml如果您以前使用过Restful Web服务,那么这个问题就足够解释了。因为长类型的列表不会被传输,您是否返回了一个
 Long
,我会说将其作为纯文本发送(将
null
映射到空字符串).great..非常感谢..您能告诉我其他方法吗?如果我的rest客户端使用其他语言,如javascript,那么它将如何再次将此用户信息更改为列表?在客户端,它不会发送
String xml=null;请尝试{StringWriter StringWriter=new StringWriter();marshaller.marshal(user,stringWriter);xml=stringWriter.toString();}catch(JAXBException ex){Logger.getLogger(UmsClient.class.getName()).log(Level.SEVERE,null,ex);}String result=client.sendPostRequestXmlData2(“user”,xml);
给出异常javax.xml.bind.JAXBException:class com.tm.ums.model.UserInfo,它的任何超类在此上下文中都是未知的。
@POST
@Path("/temp/{abc}")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
// NOTE THE CHANGE OF RESULT TYPE
public UserInfo createUser2(List<User> users,@PathParam("abc") String abc) {
    //.................//
    List<Long> listLong=new ArrayList<Long>();
    listLong.add(1L);
    listLong.add(2L);
    System.out.println("temp called");
    return new UserInfo(listLong); // <<<< THIS LINE CHANGED TOO
}