3
votes

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.

1
Please add a sample json message and add the code that calls the service. You can also try invoking your service with a rest client like postman.reos
No spam, I want your ajax request code.0gam
"This one shows the correct JSON string" That's irrelevant. What's important is the media type.a better oliver
I'm using a ReST client to call the service - RESTClient in Mozilla and Postman in Chrome. The JSON message is very simple: {"employeeId":123,"cardPin":789} In the clients the Content-Type is set to "application/json"No Spam
any luck??, I am also getting the same issueDipanshu Verma

1 Answers

0
votes

Try to set consumes = "application/json" instead of headers = {"Accept=application/json"} in @RequestMapping