[英]415 Unsupported Media Type - Spring version
I have this method on my controller: 我的控制器上有此方法:
@RequestMapping(value="/test", method = RequestMethod.POST)
public @ResponseBody String test(@RequestBody Test test) {
return test.getName();
}
My Test class: 我的测试班:
public class Test implements Serializable {
private static final long serialVersionUID = -1150931681075770764L;
private String name;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
I post this json using: 我使用以下方式发布此json:
{"name": "avocado"}
I also have this annotation: 我也有这个注释:
<mvc:annotation-driven />
I'm using Advanced Rest Client to test it. 我正在使用Advanced Rest Client进行测试。 My request's Content-Type is set to "application/json".
我的请求的Content-Type设置为“ application / json”。
If I set spring version to 4.0.9.RELEASE or earlier I get 200 OK code when post. 如果我将spring版本设置为4.0.9.RELEASE或更早版本,则发布时会得到200 OK代码。
If I set spring version to 4.1.0.RELEASE or later I get 415 Unsupported Media Type. 如果我将spring version设置为4.1.0.RELEASE或更高版本,则会得到415 Unsupported Media Type。
What shoud I do in order to get code 200 setting spring version to 4.2.1.RELEASE? 为了使代码200将spring版本设置为4.2.1.RELEASE,我应该怎么做?
I suggest you to add 我建议你添加
@RequestMapping(value="/test", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String test(@RequestBody Test test) {
return test.getName();
}
Also conversion works with the HttpMessageConverter<?>
to be able to serialize/deserialize in JSON format you need to have Jackson libraries in your classpath then spring will be able to instantiate the MappingJackson2HttpMessageConverter
转换也可以与
HttpMessageConverter<?>
一起使用,以便能够以JSON格式序列化/反序列化,您需要在类路径中具有Jackson库,然后spring就能实例化MappingJackson2HttpMessageConverter
Add this to your dependencies. 将此添加到您的依赖项。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-json.version}</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.