【问题标题】:Content type 'application/json' not supported in Spring MVC on POSTPOST 上的 Spring MVC 不支持内容类型“application/json”
【发布时间】:2017-02-23 10:10:07
【问题描述】:

我试图在 POST 方法中解析一个简单的对象,但我总是收到此错误:

2017-02-23 03:52:45 DEBUG AnnotationMethodHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG ResponseStatusExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG DefaultHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

在我的 pom.xml 中,我包含了 jackson 库文件:

...
    <properties>
        <jackson.version>2.6.3</jackson.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
...

我已经这样声明了控制器:

/**
     * 
     * @param request
     * @return
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uiupdatepost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> UIUpdatePost(@RequestBody Category category, @RequestHeader HttpServletRequest request) {
        try {
            return ResponseEntity.ok("");
        } catch (Throwable t) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new UIError(t).toHTML());
        }
    }

类分类很简单:

public class Category implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Category() {
        //
    }

    private String id;
    private String name;
    private String priority;
    private String categoryId;
    private String color;

    /**
    ALL GETTER AND SETTERS
    **/
}

如果我运行 CURL:

curl -H "Content-Type: application/json" -X POST -d '{"id":"1","name":"PIZZA","priority":"1","color":"","categoryId":""}' http://localhost:8080/food/uiupdatepost.html

我收到此错误:

HTTP:415

我已经搜索了所有可能的解决方案,包括:

  1. 删除 consumes 属性。
  2. 检查了 2 个名称相同但类型不同的 setter。不,所有属性都有 1 个 setter/1 个 getter。

还测试了手动反序列化没有问题:

    Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class);

【问题讨论】:

  • 在控制器类上方使用@RestController。

标签: java json spring spring-mvc jackson


【解决方案1】:

听起来,由于某种原因,Spring 无法创建能够解析 JSON 的适当 HTTPMessageConverter。如果 jackson 在类路径上——就像你的 pom-xml 中包含的那样——转换器应该在方法 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters

中注册

我建议检查一下,为什么这没有发生。或者,您可以手动配置适当的HttpMessageConverter。如果你有一个派生自org.springframework.web.servlet.config.annotation.WebMvcConfigurer 的配置类,你会得到这样的东西:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    converters.add(new MappingJackson2XmlHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter());
}

【讨论】:

  • 谢谢!这帮助我找到了解决方案:我错过了@EnableWebMvc。
  • 很高兴帮助你;-)
【解决方案2】:

Welp.. 我想哭... 几个小时后,改变:

MediaType.APPLICATION_JSON_VALUE

MediaType.APPLICATION_JSON_UTF8_VALUE

我的工人。

【讨论】:

    猜你喜欢
    • 2016-11-17
    • 2018-08-06
    • 2017-10-21
    • 2022-12-22
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2019-02-20
    相关资源
    最近更新 更多