内容类型' application / json' Spring MVC和jackson不支持

时间:2016-07-14 14:46:05

标签: java json spring spring-mvc

我在尝试使用Spring MVC接收发布请求时收到错误(处理程序执行导致异常:内容类型' application / json'不支持)。

我的Json,仅用于测试,非常简单:

{ "test": "abc123" }

我的pojo课程:

public class Request {

    String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

我的控制员:

@RequestMapping(value = "/testing", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
private void testing(@RequestBody Request body, @RequestHeader HttpHeaders headers, HttpServletRequest httpRequest) {
    System.out.println(body.getTest());
}

在我的pom.xml中,我添加了:

<dependencies>
    ...
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.4.3</version>
    </dependency>
</dependencies>

我认为json反序列化有问题,但我找不到它。

欢迎任何帮助。感谢。

5 个答案:

答案 0 :(得分:2)

以下是我的工作示例:

@SpringBootApplication
@Controller
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class);
  }


  @RequestMapping(value = "/testing", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  private void testing(@RequestBody Request body, @RequestHeader HttpHeaders headers, HttpServletRequest httpRequest) {
    System.out.println(body.getTest());
  }
}

这个项目只有一个依赖项:

org.springframework.boot:spring-boot-starter-web

当我这样调用网址时:

curl -XPOST -v -d '{ "test": "abc123" }' -H "Content-type: application/json" http://localhost:8080/testing

我在日志中看到了正确的abc123。如果我删除Content-type标题,我会得到异常

org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded' not supported

答案 1 :(得分:2)

在我的情况下,问题是对属性的dto的补充,其具有Jackson的错误类型,它是JsonObject类型。由于这种增加,杰克逊无法对其他物体进行反序列化 异常消息完全不准确!

答案 2 :(得分:1)

在您的webconfig类

中添加此 Bean
@Bean
public ContentNegotiatingViewResolver contentViewResolver() {
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");

    MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
    defaultView.setExtractValueFromSingleKeyModel(true);

    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver> asList(viewResolver));
    contentViewResolver.setDefaultViews(Arrays.<View> asList(defaultView));
    return contentViewResolver;
}

<强>更新

评论中的人是对的,这不会解决你的问题,但我注意到了一些事情。

如果我设置consumemes = MediaType.APPLICATION_JSON_VALUE,并且在请求中我没有指定内容类型,它将抛出异常,那么如果我设置了内容类型,问题就解决了。

现在,问题似乎与您的依赖关系有关。

我的pom依赖项:

 <properties>
     <springframework.version>4.1.9.RELEASE</springframework.version>
     <springframework.security.oauth.version>2.0.9.RELEASE</springframework.security.oauth.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    ...

</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>${springframework.security.oauth.version}</version>
    </dependency>

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

    ...

</dependencies>

答案 3 :(得分:0)

对于任何其他寻找此问题的人,我的Pojo类上必须至少有一个公共变量或吸气剂。

答案 4 :(得分:0)

我有同样的问题。终于我解决了。实际错误在jackson库中。这是位置和代码段。

/* \.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.6.5\jackson-databind-2.6.5-sources.jar!\com\fasterxml\jackson\databind\DeserializationContext.java  406 */


public boolean hasValueDeserializerFor(JavaType type, AtomicReference<Throwable> cause) {
        try {
            return _cache.hasValueDeserializerFor(this, _factory, type);
        } catch (JsonMappingException e) {
            if (cause != null) {
                cause.set(e);
            }
        } catch (RuntimeException e) {
            if (cause == null) { // earlier behavior
                throw e;
            }
            cause.set(e);
        }
        return false;
    }