java Spring MVC 和 jackson 不支持内容类型“application/json”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38377311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content type 'application/json' not supported in Spring MVC and Hymanson
提问by pedrohreis
I'm getting an error (Handler execution resulted in exception: Content type 'application/json' not supported) when trying to receive a post request using Spring MVC.
尝试使用 Spring MVC 接收发布请求时出现错误(处理程序执行导致异常:不支持内容类型“应用程序/json”)。
My Json, just for testing, is pretty simple:
我的 Json,仅用于测试,非常简单:
{ "test": "abc123" }
My pojo class:
我的 pojo 课:
public class Request {
String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
And my controller:
还有我的控制器:
@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());
}
In my pom.xml, I added:
在我的 pom.xml 中,我添加了:
<dependencies>
...
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-dataformat-xml</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
I think that something is wrong in json deserialization, but I cannot find it.
我认为json反序列化有问题,但我找不到。
Any help is welcome. Thanks.
欢迎任何帮助。谢谢。
采纳答案by Nikem
Here is my working example:
这是我的工作示例:
@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());
}
}
This project has only 1 dependency:
该项目只有 1 个依赖项:
org.springframework.boot:spring-boot-starter-web
When I call the url like this:
当我这样调用 url 时:
curl -XPOST -v -d '{ "test": "abc123" }' -H "Content-type: application/json" http://localhost:8080/testing
I see the correct abc123
in the logs. If I remove Content-type
header I get the exception
我abc123
在日志中看到正确的。如果我删除Content-type
标题,我会得到异常
org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded' not supported
回答by Wilder Valera
Add this Beanin your webconfig class
在你的 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");
MappingHymanson2JsonView defaultView = new MappingHymanson2JsonView();
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
更新
The guys from the comments are right, this won't fix your issue but I noticed something.
评论中的人是对的,这不会解决您的问题,但我注意到了一些事情。
If I set consumes = MediaType.APPLICATION_JSON_VALUE and in the request I don't specify the content type it will throw an exception then if I set the content type the issue is fixed.
如果我设置consumes = MediaType.APPLICATION_JSON_VALUE 并且在请求中我没有指定内容类型,它会抛出异常然后如果我设置内容类型,问题就解决了。
Now, the issue seems to be something with your dependencies.
现在,问题似乎与您的依赖关系有关。
My pom dependencies:
我的 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.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.3.1</version>
</dependency>
...
</dependencies>
回答by George Siggouroglou
In my case the problem was an addition to a dto of a property which having a type that was error prone for Hymanson, it was of type JsonObject. Hymanson was unable to deserialize other objects because of that addition.
The exception message is completely inaccurate!
在我的情况下,问题是添加到属性的 dto 中,该属性的类型对于 Hymanson 来说容易出错,它是 JsonObject 类型。由于该添加,Hymanson 无法反序列化其他对象。
异常信息完全不准确!
回答by Hohenheimsenberg
For anyone else looking for this problem, I had to have at least one public variable or a getter on my Pojo class.
对于寻找此问题的其他任何人,我必须在我的 Pojo 类中至少有一个公共变量或一个 getter。
回答by Sheikh Abdul Wahid
I had same issue. finally i resolved it. Actual error was in Hymanson
lib. Here is the location and code snippet.
我有同样的问题。最后我解决了它。实际错误在Hymanson
lib 中。这是位置和代码片段。
/* \.m2\repository\com\fasterxml\Hymanson\core\Hymanson-databind.6.5\Hymanson-databind-2.6.5-sources.jar!\com\fasterxml\Hymanson\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;
}