No matter what I try the request to the dispatcher servlet gives back an HTTP 415 error. The Content-Type is set to application/json in the request.
It seems the message converter is not mapping the request to the object.
I have all the Jackson dependencies in my POM:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.7</version>
</dependency>
Controller class:
@RequestMapping(value = "/login", method = RequestMethod.POST, headers = {"Accept=application/json"}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DriverLoginResponseDTO login(@RequestBody MyDTO dto)
{
System.out.println("login method hit!");
LoginResponseDTO resp = null;
try {
resp = loginService.processLogin(dto);
}
catch (Exception cde) {
resp = new LoginResponseDTO();
resp.setMessage("ERROR");
}
// Return response object
return resp;
} // login
I've tried adding the accepts, consumes, produces in the @RequestMapping to no avail.
I can get the correct JSON response with the HttpServletRequest as the method parameter:
@RequestMapping(value = "/login3", method = RequestMethod.POST)
@ResponseBody
public DriverLoginResponseDTO login3(HttpServletRequest request)
{
String line;
StringBuilder sb = new StringBuilder();
InputStream is;
try {
is = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("login3 method hit!");
System.out.println("JSON string received: " + sb.toString());
LoginResponseDTO resp = null;
try {
resp = loginService.processLogin(new MyDTO());
}
catch (Exception cde) {
resp = new LoginResponseDTO();
resp.setMessage("ERROR");
}
// Return response object
return resp;
} // login
This one shows the correct JSON string when written to System.out. Something is not mapping the JSON request to the RequestBody object in the 1st "login" method above but I cannot see what is missing.
I don't know if this is relevant but Websphere is also throwing the following error:
SRVE8094W: WARNING: Cannot set header. Response already committed.
{"employeeId":123,"cardPin":789}
In the clients the Content-Type is set to "application/json" – No Spam