2
votes

Person.java

@XmlRootElement
public class Person {
    
    private int id;
    private String fname;
    private String lname;

    // getter and setter
}

REST POST service

@POST    
@Consumes({MediaType.APPLICATION_JSON}) 
public void createPerson(JAXBElement<Person> person) {
      Person p = person.getValue();
      System.out.println("========= Person ===========");
      System.out.println(p.getFname() + " " + p.getLname());
      System.out.println("========= Person ===========");
}

OR this one

@POST    
@Consumes({MediaType.APPLICATION_JSON}) 
public void createPerson(Person person) {          
      System.out.println("========= Person ===========");
      System.out.println(person.getFname() + " " + person.getLname());
      System.out.println("========= Person ===========");
}

Test Client: Always return 400 - Bad request.

ClientConfig config = new DefaultClientConfig();
 Client client = Client.create(config);
 WebResource service = client.resource("http://localhost:8084/rest/api/person");
                              
 Person person = new Person();
 person.setId(1);
 person.setFname("John");
 person.setLname("Doe");                
 ClientResponse resp = service.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, person);
    
 System.out.println(resp.getStatus()); //Always return 400 - Bad request

Any help is much appreciated. By the way I am using jersey 1.8. I can make it work in latest Jersey version, but I need to make it work also in previous version of Jersey.

LoggingFilter output:

Mar 10, 2015 7:47:24 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > POST http://localhost:8084/rest/api/person
1 > Content-Type: application/json
{"fname":"d","id":"0","lname":"d"}

Mar 10, 2015 7:47:24 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 400
1 < Date: Mon, 09 Mar 2015 23:47:24 GMT
1 < Content-Length: 1004
1 < Connection: close
1 < Content-Type: text/html;charset=utf-8
1 < Server: Apache-Coyote/1.1
1 < 
<html><head><title>Apache Tomcat/7.0.27 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Bad Request</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Bad Request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect (Bad Request).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.27</h3></body></html>

Apache Tomcat Server Log output: (But I don't think it has implication in the 400 - Bad Request?)

Mar 10, 2015 7:29:20 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /billing threw load() exception
javax.servlet.ServletException: missing jspFile
    at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:123)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1585)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

Dependencies (JARS):

  • activation.jar
  • asm-3.1.jar
  • commonj.sdo-2.1.1
  • eclipselink-2.6.0-RC1
  • jackson-jaxrs-1.7.1
  • javax.json-1.0.4
  • javax.persistence-2.1.0
  • jaxb-api
  • jersey-apache-client-1.8
  • jersey-bundle-1.8
  • jersey-client-1.8
  • jersey-core-1.8
  • jersey-json-1.8
  • jersey-server-1.8
  • jsr173_1.0_api
  • jstl-1.2
  • org.eclipse.persistence.moxy-2.6.0-RC1
  • servlet-api-2.5
  • validation-api-1.1.0.Final
1
Also add the logging filter and see if any useful information comes up. client.addFilter(new LoggingFilter()); - Paul Samsotha
Also can you show your dependencies - Paul Samsotha
@peeskillet, please check above logging filter and tomcat server log output and the included JARS on my project. - imprezzeb
The error in the message body says "The request sent by the client was syntactically incorrect", but I really don't see anything wrong. I've tested this (with the Person param method) and it works fine for me. - Paul Samsotha
If you have a running project that you can post to github or something, I can test it out, but I cannot currently reproduce the problem with what you've posted. Check that it's not a problem with Tomcat. Maybe try it on another container. I don't know what that exception in Tomcat means. Doesn't really make any sense. But it doesn't look like it even has anything to do with Jersey, and it looks to be happening on start up, so I don't think it's request related. - Paul Samsotha

1 Answers

0
votes

Your createPerson() method is not annotated with @Path("/person") annotation, where Path is of type javax.ws.rs.Path and your service interface also needs to be annotated with @Path("/api")