imprezzeb
imprezzeb

Reputation: 716

Always getting 400 - Bad request when posting data in a REST/JAX-RS

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):

Upvotes: 1

Views: 11243

Answers (1)

Charu Khurana
Charu Khurana

Reputation: 4551

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")

Upvotes: 0

Related Questions