[英]Content type 'application/json' not supported in Spring MVC and jackson
尝试使用 Spring MVC 接收发布请求时出现错误(处理程序执行导致异常:不支持内容类型“应用程序/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反序列化有问题,但我找不到。
欢迎任何帮助。 谢谢。
这是我的工作示例:
@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());
}
}
该项目只有1个依赖项:
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
在我的情况下,问题是属性的dto的添加,该属性的类型对于Jackson来说很容易出错,类型是JsonObject。 由于添加了杰克逊,因此无法反序列化其他对象。
异常消息是完全不正确的!
in your webconfig class 将此添加到您的webconfig类中
@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;
}
UPDATE
评论中的人是正确的,这不会解决您的问题,但我注意到了一些问题。
如果设置了消耗= 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>
对于寻找此问题的其他任何人,我的Pojo类上必须至少有一个公共变量或吸气剂。
我有同样的问题。 终于我解决了。 实际错误出在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;
}
对于那些使用Spring Framework而不是Spring Boot 的人,遇到了这个问题。
这个问题的根源是:
Spring需要①识别“Content-Type”,②将内容转换为我们在方法签名中声明的参数类型。
不支持“ application/json ”,因为默认情况下,Spring 找不到合适的 HttpMessageConverter来完成转换工作,即步骤②。
解决方案:
脚步:
选择我们要使用的HttpMessageConverter 类。 对于JSON,我们可以从“org.springframework.http.converter.json。JsonbHttpMessageConverter”, “org.springframework.http.converter.json。MappingJackson2HttpMessageConverter”选择,等等。
通过在我们的应用程序中调用“WebMvcConfigurer”实现类的“public void configureMessageConverters (List<HttpMessageConverter<?>>converters)”方法,将JsonbHttpMessageConverter对象(或其他类型的Json HttpMessageConverter对象)添加到Spring的配置中。 在方法内部,我们可以根据需要添加任何 HttpMessageConverter 对象,通过使用“ converters.add() ”。
将相应的依赖添加到“pom.xml”文件中。 例如,如果使用“ JsonbHttpMessageConverter ”,则需要添加“ javax.json.bind-api ”和“ yasson ”的依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.