简体   繁体   English

POST上的Spring MVC不支持内容类型'application / json'

[英]Content type 'application/json' not supported in Spring MVC on POST

im trying to parse a simple object in a POST method but I'm always getting this error: 我试图在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

In my pom.xml I've included the jackson library files: 在我的pom.xml中,我包含了杰克逊库文件:

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

And I've declared the controller this way: 我以这种方式声明了控制器:

/**
     * 
     * @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());
        }
    }

The class category is very simple: 类类别非常简单:

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
    **/
}

If I run CURL: 如果我运行CURL:

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

I get this error: 我收到此错误:

HTTP: 415 HTTP:415

I've searched all the possible solutions included: 我搜索了所有可能的解决方案,包括:

  1. Removing consumes attribute. 删除消耗属性。
  2. Checked for 2 setter with same name but different type. 检查2个名称相同但类型不同的二传手。 No, 1 setter/1 getter for all attributes. 否,所有属性的1个setter / 1 getter。

Also tested the manual deserialization without problems: 还测试了手动反序列化没有问题:

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

Sounds, that Spring wasn't able to create an appropriate HTTPMessageConverter able to parse JSON for some reason. 听起来,由于某种原因,Spring无法创建能够解析JSON的适当HTTPMessageConverter。 If jackson is on the classpath - as it is as contained in your pom-xml - the converter should be registered in method org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters 如果jackson在类路径上(如pom-xml中所包含的那样),则转换器应在org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters方法中注册

I'd suggest to check, why this isn't happening. 我建议检查一下,为什么不发生这种情况。 Alternatively you can configure appropriate HttpMessageConverter by hand. 另外,您可以手动配置适当的HttpMessageConverter If you've a configuration class derived from org.springframework.web.servlet.config.annotation.WebMvcConfigurer you would have something like this: 如果您有一个从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());
}

Welp.. I want to cry... after hours and hours, changing : Welp ..我想哭……几个小时后,改变:

MediaType.APPLICATION_JSON_VALUE MediaType.APPLICATION_JSON_VALUE

for 对于

MediaType.APPLICATION_JSON_UTF8_VALUE MediaType.APPLICATION_JSON_UTF8_VALUE

worker for me. 我的工人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC - 不支持内容类型“应用程序/json” - Spring MVC - Content type 'application/json' not supported Spring MVC 和 jackson 不支持内容类型“application/json” - Content type 'application/json' not supported in Spring MVC and jackson spring boot mvc - 不支持内容类型'application / json; charset = UTF-8' - spring boot mvc - Content type 'application/json;charset=UTF-8' not supported Spring Boot POST PDF内容类型&#39;application / pdf不支持 - Spring boot POST PDF Content type 'application/pdf not supported Spring Rest POST Json RequestBody 内容类型不支持 - Spring Rest POST Json RequestBody Content type not supported Spring WebClient put Mapping:不支持内容类型&#39;application / json&#39; - Spring WebClient put Mapping: Content type 'application/json' not supported Spring MVC 4:“application/json”内容类型设置不正确 - Spring MVC 4: “application/json” Content Type is not being set correctly 不支持获取内容类型“application/json” - Getting Content type 'application/json' not supported Spring Rest 应用程序中的“不支持内容类型‘application/json;charset=UTF-8’” - "Content type 'application/json;charset=UTF-8' not supported" in Spring Rest application 当我尝试将 JSON 发送到 Spring 时,不支持内容类型“application/json;charset=UTF-8” - Content type 'application/json;charset=UTF-8' not supported, when i try send JSON to Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM