简体   繁体   中英

Getting Content type 'application/json' not supported

I am getting below exception when i am using @RequestBody in my controller class.

But I need to use @RequestBody only for post method.

Caused by: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

Anyone can help how to solve this issue..

you can add this annotation

@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON)

or put this dependency

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.4.3</version>
</dependency>

I was getting this in my unit tests, even though I had all the correct annotations on the Controller methods, and I was correctly setting the Content-Type in my request.

Error - Caused by: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

After debugging AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters , I realized there were no registered message converters that supported application/json .

@AutoConfigureMockMvc doesn't register all the converters, but @WebMvcTest does.
I changed the test annotation, and the problem was solved.

Previously I was using -

@SpringJUnitWebConfig
@ContextConfiguration
@AutoConfigureMockMvc
public class ReportControllerTests {

Now using the @WebMvcTest which registers the additional message converters.

@SpringJUnitWebConfig
@ContextConfiguration
@WebMvcTest(controllers = ReportController.class)
public class ReportControllerTests {

Firstly, Make sure that you have a correctly jackson version using:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.8.0</version>
 </dependency>
 <dependency>
     <artifactId>jackson-annotations</artifactId>
     <groupId>com.fasterxml.jackson.core</groupId>
     <version>2.8.0</version>
 </dependency>

As for me, when I use the version of v2.7.6 or v2.8.7. This error came to me! And I change to v2.8.0, It work well!

Secondly, Make sure your controller code:

@RestController
@RequestMapping(value = "/user", produces = {MediaType.APPLICATION_JSON}, consumes = {MediaType.APPLICATION_JSON})
public class UserController extends GenericController<User, String> {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage add(@RequestBody User user) {
       String id = getService().insert(user);
       return ResponseMessage.created(id );
    }
}

RequestBody:

{
    "username":"123",
    "password":"123",
    "name":"123",
    "phone":"123",
    "email":"123@qq.com",
    "status":1
}

Postman :

Postman Post

Hope it helps you!

Do you have consumes="application/json" attribute in the @RequestMapping. If not please put that in the annotation and then try. Also if that deosnt work, try using contentNegotiator, you can google it for contentNegotiation for spring.

在@PutMapping 中尝试没有消费者和生产者选项。

@PutMapping(value = "/lists/{ListName}/item")

add jackson-databind in pom.xml solved my problem:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.1</version>
        </dependency>

there is NO need to add consumes: consumes = MediaType.APPLICATION_JSON_VALUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM