Reputation: 169
I'm attempting to build a POST method for a Java web service utilising Jersey RS that consumes JSON, however I am receiving a 400 bad request error when I submit some sample JSON.
@Path("/Register")
@Stateless
public class RegistrationController {
@POST
@Path("/Test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String postHelloWorld(String message){
return "temp";
}
Above is the code for the post method, and when I attempt to use an API service such as Postman to submit a post request to
http://localhost:37846/Register/Test
Using Postman to set a header of Content-Type as application\json, with some sample JSON such as
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
I receive the 400 bad request error.
Response body:
<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>
Attempting to print the message using System.out failed, as did a generic system.out.print("hello") message, which leads me to believe that the method is not being called.
Since I'm not receiving a 404 error, and when I tweak the data types I'll receive a 415 Unsupported Media type error, this rules out something such as incorrect mapping.
JSON is not malformed, so problem likely lies with either the submitted request or how the web service handles the request.
Upvotes: 4
Views: 23588
Reputation: 169
Problem was a typo in the header.
Content-Type application\json
Was what was causing the 400 error.
The header that resolves the problem is
Content-Type application/json
Upvotes: 3