1

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
akop
  • 5,981
  • 6
  • 24
  • 51
imprezzeb
  • 706
  • 1
  • 7
  • 18
  • Also add the logging filter and see if any useful information comes up. `client.addFilter(new LoggingFilter());` – Paul Samsotha Mar 09 '15 at 08:41
  • Also can you show your dependencies – Paul Samsotha Mar 09 '15 at 08:41
  • @peeskillet, please check above logging filter and tomcat server log output and the included JARS on my project. – imprezzeb Mar 09 '15 at 23:59
  • 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 Mar 10 '15 at 01:12
  • 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 Mar 10 '15 at 01:15
  • Another thing, `jersey-json` is dependent on other jars, which you don't have, Jackson, being the most important of those. `jackson-core-asl-1.7.1`, `jackson-mapper-asl-1.7.1`, `jackson-xc-1.7.1`, `jaxb-api-2.2.2`, `jaxb-impl-2.2.3-1`, `jettison-1.1`, `stax-api-1.0.1`, `stax-api-1.0.2`. There are others, but you already have them. This is one of the many reasons you should use a build tool like Maven. Just adding `jersey-json` as a dependency, would have pulled all those in. if you don't know Maven, I strongly urge you to learn it. – Paul Samsotha Mar 10 '15 at 02:07
  • @peeskillet, yes I've used Maven and Gradle in my projects, but this one is still using ant. I tried to create a very simple project with the above JARS I've listed and it worked, I just relocated my jaxb.properties to the upper level package. But when I tried it also in the real project it doesn't. I really think it is a conflict in some of the JARS which i have o find out first. I'll let you know once I figure it out. – imprezzeb Mar 10 '15 at 03:01
  • If you think it's a conflict with dependencies, try this, create a new Maven project, and just add these dependencies `jersey-server`, `jersey-json`, `jersey-client`. [Configure the app](https://jersey.java.net/documentation/1.18/user-guide.html#d4e188). [Configure the JSON support](https://jersey.java.net/documentation/1.18/user-guide.html#json.pojo.approach.section). Create a simple resource using JSON/POJO. Test it out. If it works (which is should as I've tested), then compare the dependencies with your project, and take out what you don't need or add what you need. – Paul Samsotha Mar 10 '15 at 03:18
  • All the groupId's are `com.sun.jersey` and version `1.8` – Paul Samsotha Mar 10 '15 at 03:22
  • Alternatively, you can use a Maven archetype, with coordinates `com.sun.jersey.archetypes : jersey-quickstart-webapp : 1.8`. This should get a webapp up and running the quickest, with the above mentioned dependencies and web.xml configuration – Paul Samsotha Mar 10 '15 at 03:25
  • My problem is the same with this post http://stackoverflow.com/questions/15435899/getting-400-bad-request-from-same-application-on-weblogic-on-red-hat, I tried to change it from using JAXBElement to String and it works. – imprezzeb Mar 12 '15 at 23:06
  • Tried to create new maven project and used JAXBElement and all is well. – imprezzeb Mar 12 '15 at 23:12
  • Aha, using @Consumes({MediaType.APPLICATION_JSON}), JAXBElement will not work. But if you use @Consumes({MediaType.APPLICATION_XML}), then it will. I think JAXB has problem marshalling JSON to JAXBElement? – imprezzeb Mar 12 '15 at 23:28

1 Answers1

0

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

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
  • createPerson() method is just one of the function inside my REST web service class and it is annotated with @Path("/person"), and /api/* is already defined in deployment descriptor (web.xml). Btw, the method (GET) inside are working fine. This is I think a conflict in dependencies. – imprezzeb Mar 09 '15 at 22:41