我试图在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

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


删除消耗属性。
检查2个名字相同但类型不同的二传手。否,所有属性均使用1个setter / 1个getter。


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

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

最佳答案

听起来,由于某种原因,Spring无法创建适当的HTTPMessageConverter来解析JSON。如果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());
}

08-26 10:17