Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content type 'application/json' not supported in Spring MVC and jackson

I'm getting an error (Handler execution resulted in exception: Content type 'application/json' not supported) when trying to receive a post request using Spring MVC.

My Json, just for testing, is pretty simple:

{ "test": "abc123" }

My pojo class:

public class Request {

    String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

And my controller:

@RequestMapping(value = "/testing", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
private void testing(@RequestBody Request body, @RequestHeader HttpHeaders headers, HttpServletRequest httpRequest) {
    System.out.println(body.getTest());
}

In my pom.xml, I added:

<dependencies>
    ...
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.4.3</version>
    </dependency>
</dependencies>

I think that something is wrong in json deserialization, but I cannot find it.

Any help is welcome. Thanks.

like image 467
pedrohreis Avatar asked Dec 18 '22 15:12

pedrohreis


1 Answers

In my case the problem was an addition to a dto of a property which having a type that was error prone for Jackson, it was of type JsonObject. Jackson was unable to deserialize other objects because of that addition.
The exception message is completely inaccurate!

like image 132
Georgios Syngouroglou Avatar answered Feb 16 '23 00:02

Georgios Syngouroglou