Question

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.

Was it helpful?

Solution

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) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top