java 不支持获取内容类型“应用程序/json”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29650483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:40:01  来源:igfitidea点击:

Getting Content type 'application/json' not supported

javaspringspring-mvcresttemplate

提问by Java Learing

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

当我@RequestBody在我的控制器类中使用时,我遇到了异常。

But I need to use @RequestBodyonly for post method.

但我@RequestBody只需要用于 post 方法。

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

引起:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“application/json”

Anyone can help how to solve this issue..

任何人都可以帮助如何解决这个问题..

回答by Akash Chavda

you can add this annotation

您可以添加此注释

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

or put this dependency

或者把这个依赖

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

回答by Allen

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

首先,使用以下方法确保您拥有正确的 Hymanson 版本:

<dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-core</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
     <groupId>com.fasterxml.Hymanson.core</groupId>
     <artifactId>Hymanson-databind</artifactId>
     <version>2.8.0</version>
 </dependency>
 <dependency>
     <artifactId>Hymanson-annotations</artifactId>
     <groupId>com.fasterxml.Hymanson.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!

至于我,当我使用v2.7.6或v2.8.7的版本时。这个错误来找我!我改成v2.8.0,效果很好!

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":"[email protected]",
    "status":1
}

Postman :

邮递员:

Postman Post

邮递员邮局

Hope it helps you!

希望对你有帮助!

回答by Jeremy

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.

我在我的单元测试中得到了这个,即使我在 Controller 方法上有所有正确的注释,并且我在我的请求中正确设置了 Content-Type。

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

错误 - 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.

调试后AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters,我意识到没有注册的消息转换器支持application/json.

@AutoConfigureMockMvcdoesn't register all the converters, but @WebMvcTestdoes.
I changed the test annotation, and the problem was solved.

@AutoConfigureMockMvc不会注册所有转换器,但@WebMvcTest会注册。
我修改了测试注解,问题解决了。

Previously I was using -

以前我使用 -

@SpringJUnitWebConfig
@ContextConfiguration
@AutoConfigureMockMvc
public class ReportControllerTests {

Now using the @WebMvcTestwhich registers the additional message converters.

现在使用@WebMvcTest注册附加消息转换器的 。

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

回答by chetank

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.

@RequestMapping 中是否有consumes="application/json" 属性?如果没有,请将其放在注释中,然后尝试。此外,如果该 deosnt 工作,请尝试使用 contentNegotiator,您可以在 google 上搜索 contentNegotiation for spring。