java中的RESTful Web服务

Imr*_*ram -1 java xml rest post

我试图Long在我的资源中传递列表作为发布数据和消费类型是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
Run Code Online (Sandbox Code Playgroud)

Don*_*ows 5

问题是没有转换代码知道如何自动更改LongList<Long>转换为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;
    }
}
Run Code Online (Sandbox Code Playgroud)
@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
}
Run Code Online (Sandbox Code Playgroud)