简体   繁体   English

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

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

Person.java人.java

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

    // getter and setter
}

REST POST service 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 ===========");
}

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.测试客户端:始终返回 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

Any help is much appreciated.任何帮助深表感谢。 By the way I am using jersey 1.8.顺便说一句,我使用的是球衣 1.8。 I can make it work in latest Jersey version, but I need to make it work also in previous version of Jersey.我可以让它在最新的 Jersey 版本中工作,但我需要让它在以前版本的 Jersey 中也能工作。

LoggingFilter output: 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 Server Log output: (But I don't think it has implication in the 400 - Bad Request?) Apache Tomcat 服务器日志输出:(但我不认为它对 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):依赖项(JARS):

  • activation.jar激活.jar
  • asm-3.1.jar asm-3.1.jar
  • commonj.sdo-2.1.1 commonj.sdo-2.1.1
  • eclipselink-2.6.0-RC1 eclipselink-2.6.0-RC1
  • jackson-jaxrs-1.7.1杰克逊-jaxrs-1.7.1
  • javax.json-1.0.4 javax.json-1.0.4
  • javax.persistence-2.1.0 javax.persistence-2.1.0
  • jaxb-api jaxb-api
  • jersey-apache-client-1.8 jersey-apache-client-1.8
  • jersey-bundle-1.8球衣捆绑包 1.8
  • jersey-client-1.8 jersey-client-1.8
  • jersey-core-1.8球衣核心 1.8
  • jersey-json-1.8球衣-json-1.8
  • jersey-server-1.8 jersey-server-1.8
  • jsr173_1.0_api jsr173_1.0_api
  • jstl-1.2 jstl-1.2
  • org.eclipse.persistence.moxy-2.6.0-RC1 org.eclipse.persistence.moxy-2.6.0-RC1
  • servlet-api-2.5 servlet-api-2.5
  • validation-api-1.1.0.Final验证-api-1.1.0.Final

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM