0

I have an issue posting some JSON to a web service built using Jersey REST. The data never seems to arrive at the server.

The overall task flow is somewhat like this:

Javascript POST JSON -> Webserver -> webserver get JSON and print out

Here is the Javascript that is supposed to POST the JSON data to the server, with path /posttest:

<script type="text/javascript">
$(function() {
    var name = "Tony";
    $.post(
            '/posttest',

            function (data) {
                alert("sune " + data.name);
            });
})
 </script>

I have a webserver (Grizzly) running all these classes:

This class uses JAX-RS API to process POST requests that consume JSON data:

@Path("/posttest")
public class PostTest {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response doPost(StatusBean b) {
    System.out.println("---> " + b.getName());
    StatusBean statusBean = new StatusBean();
    return Response.status(Response.Status.ACCEPTED).entity(statusBean).build();
}

Here is the definition of the bean that data will be serialized to and from:

@XmlRootElement
public class StatusBean {
   private String name;

   public StatusBean() {
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name= name;
   }
}

Here is the test client class that print out the data in the console

ClientConfig cc = new DefaultClientConfig();
Client c = Client.create(cc);
WebResource r = c.resource("http://localhost:9998/posttest");
StatusBean post = r.type(MediaType.APPLICATION_JSON_TYPE).post(StatusBean.class, new StatusBean());
System.out.println("--> " + post.getName());
Gray
  • 7,050
  • 2
  • 29
  • 52
nihulus
  • 1,475
  • 4
  • 24
  • 46
  • This is not compilable code. Did you cut and paste or type it all to the question by hand? In particular, in your web service implementation class, you call a method incorrectly, and reference a non existent field. Please post the correct code. – Perception Apr 19 '12 at 11:49
  • Edited! tell me if anywhere is still unclear. I pasted the necessary part of the code from my project. – nihulus Apr 19 '12 at 11:57

2 Answers2

0

Your StatusBean should have field named name, not dat - it should be consistent with {"name":"Tony"}.

BTW. What is your REST resources mapping for RESTEasy servlet? In the above setup it should be /*.

Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77
  • Ty for reply, now i updated the Javascript, thats just send a String, but got weird error message: ALLVARLIG: A message body reader for Java class StatusBean, and Java type class StatusBean, and MIME media type application/octet-stream was not found. Im not using RESTEasy tho, do i have to use it? – nihulus Apr 19 '12 at 11:49
  • Set content type in your AJAX call to application/json - http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttype-application-json – Piotr Kochański Apr 19 '12 at 12:02
  • @nihulus - In your Java client test, use `MediaType.APPLICATION_JSON` instead of `MediaType.APPLICATION_JSON_TYPE`. I edited your web service code slightly, since it still had that method reference error in it. – Perception Apr 19 '12 at 12:07
  • @Perception - I have getting the weird error message MIME media type application/octet-stream was not found. cant figur out what kind of error it is. Wourld be great if u know any example of posting json. – nihulus Apr 19 '12 at 12:19
0

I have only experience with resteasy and not jersey, but I will try to rely on the JAX-RS standard.

You should always investigate the HTTP response in more details when such errors occurs. Open the network monitor in the browser and check the HTTP response code. I think you get a 405 as you have a mismatch between the JSON sent and the StatusBean java object. You java bean have a 'dat' property while your JSON have a 'name' property.

Also make sure that you are serializing java objects straightforward similar to javascript. This can be achieved using the jackson serializor. To use this simply include it in you class path. Usgin maven this could be:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>

This link might be helpful: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

werk
  • 36
  • 3
  • Ty for reply! I edited out the code now that just posting a String. – nihulus Apr 19 '12 at 12:09
  • Eventually, try to take one step at the time. Try remove the argument for a while and ensure that you can actually return/produce the right JSON. It seems to me that your object mapper (serializer) is not set up properly. – werk Apr 19 '12 at 12:31
  • Yeah, thats what im afraid of, not sure how to set it up correctly. – nihulus Apr 19 '12 at 13:06
  • Are you using the Jackson mapper and have you added the init-parem `com.sun.jersey.api.json.POJOMappingFeature` to `web.xml` as explained in the linked blog post? – werk Apr 19 '12 at 13:10