400 POST服务上的错误请求

时间:2016-12-27 17:48:19

标签: java

我尝试使用消耗JSON的Jersey RS为Java Web服务构建POST方法,但是当我提交一些示例JSON时,我收到400错误的请求错误。

@Path("/Register")
@Stateless
public class RegistrationController {

    @POST
    @Path("/Test")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String postHelloWorld(String message){
        return "temp";
    }

以上是post方法的代码,当我尝试使用Postman等API服务向

提交帖子请求时

http://localhost:37846/Register/Test

使用Postman将Content-Type的标题设置为application \ _json,并使用一些示例JSON,例如

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

我收到400错误请求错误。

回复机构:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>GlassFish Server Open Source Edition 4.1.1 - Error report</title>
    <style type="text/css">
        <!--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;}HR {color : #525D76;}-->
</style>
</head>

<body>
    <h1>HTTP Status 400 - Bad Request</h1>
    <hr/>
    <p><b>type</b> Status report</p>
    <p><b>message</b>Bad Request</p>
    <p><b>description</b>The request sent by the client was syntactically incorrect.</p>
    <hr/>
    <h3>GlassFish Server Open Source Edition  4.1.1 </h3>
</body>

</html>

尝试使用System.out打印消息失败,通用system.out.print(&#34; hello&#34;)消息也是如此,这使我相信该方法未被调用。 /> 由于我没有收到404错误,而且当我调整数据类型时,我会收到415不支持的媒体类型错误,这会排除诸如映射不正确之类的内容。
JSON没有格式错误,因此问题可能在于提交的请求或Web服务如何处理请求。

1 个答案:

答案 0 :(得分:1)

问题是标题中的拼写错误。

Content-Type application\json

导致400错误的是什么 解决问题的标题是

Content-Type application/json