2016-12-27 225 views
3

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

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

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

以上是为后方法的代码,并且当我试图使用API​​服务诸如邮递员提交POST请求

http://localhost:37846/Register/Test

使用邮差设置的报头Content-Type as 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( “你好”)消息的消息,这使我相信该方法没有被调用。
由于我没有收到404错误,当我调整数据类型时,我将收到415不支持的媒体类型错误,这就排除了诸如不正确的映射之类的内容。
JSON不是格式错误,所以问题可能在于提交的请求或Web服务如何处理请求。

+1

调试程序!打印输入的消息。请求是否适用于不使用JSON的方法? – ppasler

+2

400错误请求表示/测试端点期望的参数不同于您发送的参数。您是否使用JSON对象映射器(如Jackson)?如果是这样,您需要在JSON中创建一个包含与菜单相同值的对象(Id,value,popup) –

+1

400意味着您大部分时间都发送了错误的数据。你确定没有错误描述的响应主体吗?一个好的服务应该有它。 – cen

回答

1

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

Content-Type application\json 

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

Content-Type application/json