总是得到400 - 在REST / JAX-RS中发布数据时的错误请求

时间:2015-03-09 07:42:27

标签: java rest jersey jax-rs

Person.java

@XmlRootElement
public class Person {

    private int id;
    private String fname;
    private String lname;
    //private Address address = new Address();

    public Person() {        
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }
}

REST POST服务

@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 ===========");
}

或者这个

@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 ===========");
}

测试客户端:始终返回400 - 错误请求。

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

非常感谢任何帮助。顺便说一句,我使用的是1.8号球衣。我可以使它在最新泽西版本中运行,但我需要在之前版本的Jersey中使它工作。

LoggingFilter输出:

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服务器日志输出:(但我认为它不会影响400 - 错误请求吗?)

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)

依赖关系(JARS):

  • 的activation.jar
  • ASM-3.1.jar
  • 为commonj.sdo-2.1.1
  • 的EclipseLink-2.6.0-RC1
  • 杰克逊 - JAXRS-1.7.1
  • javax.json-1.0.4
  • javax.persistence-2.1.0
  • JAXB-API
  • Jersey的Apache的客户端 - 1.8
  • 球衣束-1.8
  • Jersey的客户端 - 1.8
  • 球衣核-1.8
  • Jersey的JSON-1.8
  • 球衣 - 服务器 - 1.8
  • jsr173_1.0_api
  • JS​​TL-1.2
  • org.eclipse.persistence.moxy-2.6.0-RC1
  • servlet的API-2.5
  • validation-api-1.1.0.Final

1 个答案:

答案 0 :(得分:0)

您的createPerson()方法未使用@Path("/person")注释进行注释,其中Path的类型为javax.ws.rs.Path,您的服务接口也需要使用@Path("/api")进行注释