我有一个返回json响应的spring webservice。我使用这里给出的示例来创建服务:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
json返回格式为:{"name":null,"staffName":"kfc-kampar","smith"}
我希望从返回的响应中删除任何空对象,使其看起来像这样:{"staffName":"kfc-kampar","smith"}
我在这里发现了类似的问题,但我已经找到了一个有效的解决方案。
Configuring ObjectMapper in Spring
configuring the jacksonObjectMapper not working in spring mvc 3
how to configure spring mvc 3 to not return "null" object in json response?
Spring configure @ResponseBody JSON format
Jackson+Spring3.0.5 custom object mapper
通过阅读这些和其他来源,我发现实现我想要的最干净的方法是使用Spring3.1和可以在mvc注释中配置的消息转换器。我更新的spring配置文件是:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="true" />
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper">
<bean class="org.codehaus.jackson.map.ObjectMapper">
<property name="serializationInclusion" value="NON_NULL"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
服务类与mkyong.com站点上提供的服务类相同,只是我注释掉了Shop name变量的设置,因此它是空的。
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
//shop.setName(name);
shop.setStaffName(new String[]{name, "cronin"});
return shop;
}
}我使用的Jackson jars是jackson-mapper-asl 1.9.0和jackson-core-asl 1.9.0。这些是我从mkyong.com下载的spring-json项目中唯一添加到pom中的新jars。
项目成功构建,但是当我通过浏览器调用服务时,我仍然得到相同的东西,即{"name":null,"staffName":"kfc-kampar","smith"}
有人能告诉我我的配置哪里出了问题吗?
我已经尝试了其他几种方法,但我能够以正确格式返回json的唯一方法是将对象映射器添加到JSONController中,并让"getShopInJSON“方法返回一个字符串。
public @ResponseBody String getShopInJSON(@PathVariable String name) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
Shop shop = new Shop();
//shop.setName(name);
shop.setStaffName(new String[]{name, "cronin"});
String test = mapper.writeValueAsString(shop);
return test;
}现在,如果我调用该服务,我会得到预期的结果,即{"staffName":"kfc-kampar","cronin"}
我也可以使用@JsonIgnore注解让它工作,但是这个解决方案不适合我。
我不明白为什么它可以在代码中工作,但不能在配置中工作,所以任何帮助都会很棒。
发布于 2014-12-17 04:07:16
从Jackson 2.0开始,你可以使用JsonInclude
@JsonInclude(Include.NON_NULL)
public class Shop {
//...
}发布于 2016-04-09 17:58:23
由于使用了Jackson,您必须将其配置为Jackson属性。对于Spring Boot REST服务,您必须在application.properties或application.yml中对其进行配置
spring.jackson.default-property-inclusion = NON_NULL发布于 2015-12-31 14:31:04
@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
public class Shop {
//...
}对于jackson 2.0或更高版本,请使用@JsonInclude(Include.NON_NULL)
这将删除空对象和null对象。
https://stackoverflow.com/questions/12707165
复制相似问题