java Spring Rest JSON 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5624374/
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
Spring Rest JSON Binding
提问by Sri
I'm trying to create a Restful service with Spring.
我正在尝试使用 Spring 创建一个 Restful 服务。
A method accepts a "UserContext" object via argument i.e. @RequestBody.
方法通过参数即@RequestBody 接受“UserContext”对象。
The client sends the JSON object with content-type "application/json". But I get the error "HTTP/1.1 415 Unsupported Media Type".
客户端发送内容类型为“application/json”的 JSON 对象。但我收到错误“HTTP/1.1 415 不支持的媒体类型”。
..even when the client sends a null "{}" JSON object.
..即使客户端发送一个空的“{}”JSON 对象。
My controller:
我的控制器:
@Controller
@RequestMapping(value = "/entityService")
class RestfulEntityService {
@Resource
private EntityService entityService;
@ResponseBody
@RequestMapping(value = "/getListOfEntities", method = RequestMethod.POST)
public List<Entity> getListOfEntities(@RequestBody UserContext userContext) {
System.out.println(userContext);
return null;
}
}
UserContext.java
用户上下文.java
public class UserContext {
private Long userId;
private String userName;
private UserAddress userAddress;
private CustomerInfo customerInfo;
}
Application context:
应用上下文:
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
<bean id="xmlMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="xstreamMarshaller"/>
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="xmlMessageConverter" />
<ref bean="jsonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<mvc:annotation-driven/>
Struggling with this for a while. Help will be appreciated!
为此苦苦挣扎了一段时间。帮助将不胜感激!
回答by Biju Kunjummen
Try with a Accept
header in your request of application/json
, based on what I see with the messageconverter samples at mvc-showcase
根据我在mvc-showcase 中看到的 messageconverter 示例,尝试Accept
在您的请求中使用标头application/json
This is a related question: use spring mvc3 @ResponseBody had 415 Unsupported Media Type why?
这是一个相关的问题:使用 spring mvc3 @ResponseBody has 415 Unsupported Media Type 为什么?
回答by StaxMan
This is probably not the main problem, but your UserContext bean would not work as is, if it only has private fields. There are multiple ways to resolve this; from making fields public, to adding @JsonProperty for each, or just changing minimum visibility Hymanson uses for detecting property fields (@JsonAutoDetect annotation).
这可能不是主要问题,但如果 UserContext bean 只有私有字段,则它不会按原样工作。有多种方法可以解决这个问题;从公开字段,到为每个字段添加 @JsonProperty,或者只是更改 Hymanson 用于检测属性字段的最小可见性(@JsonAutoDetect 注释)。
But with empty JSON, this should not give problems; and if there was an issue, you should see different kind of error/exception (I assume).
但是对于空的 JSON,这应该不会出现问题;如果出现问题,您应该会看到不同类型的错误/异常(我假设)。
回答by gouki
Make sure that you have the Hymanson libraries on your classpath, If you're using maven, define the following on your pom.xml:
确保您的类路径上有 Hymanson 库,如果您使用的是 maven,请在 pom.xml 上定义以下内容:
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-core-asl</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>