Can jersey clients POST a JAXB object to the server using JSON?

点点圈 提交于 2020-01-14 06:32:34

问题


I'm finding a lot of examples of how to set up a jersey server so that it can produce and consume JAXB bound objects but I'm having trouble finding examples of how to get the client to post the same JAXB bound object. This example shows how to do it with XML. I'm looking for one that shows how to do it with JSON.

I'm not even sure if this is possible to do. The javadoc on the post method(s) are ambiguous.

My post looks like this:

    Client client = Client.create();
    WebResource resource = client.resource(uri);
    ClientResponse response = resource.type(MediaType.APPLICATION_JSON)
      .post(ClientResponse.class, instanceWithXmlRootElementAnnotation);

When I try this, my server gets the request, but the field for the @FormParam is always sent over as null. Here's the signature of my server side method:

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON)
public String postAPath(@FormParam("InstanceWithXmlRootElementAnnotation")
  InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) {
//instanceWithXmlRootElementAnnotation is always null

Something else I'm wondering is if I should be using instanceWithXmlRootElementAnnotation. If this were a traditional webservice, I would use JAXB to generate an object for the client to use and send over the generated class. But from what I gather from the example I linked to, the guy is sending over the source, not the generated class.


回答1:


I figured it out on my own. The problem was a server side issue. Once I removed the @FormParam annotation on the server, everything worked as expected. The combined question and answer will provide a rudimentary tutorial for others. My server now looks like:

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON)
public String postAPath(InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) {


来源:https://stackoverflow.com/questions/18665617/can-jersey-clients-post-a-jaxb-object-to-the-server-using-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!