我有一个spring webservice,它返回一个json响应.我正在使用此处给出的示例来创建服务:http: //www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
返回json的格式为:{"name":null,"staffName":["kfc-kampar","smith"]}
我想从返回的响应中删除任何空对象,所以它看起来像这样:{"staffName":["kfc-kampar","smith"]}
我在这里找到了类似的问题,但我已经能够得到一个解决方案,例如
如何在使用基于spring注释的配置时配置MappingJacksonHttpMessageConverter?
配置jacksonObjectMapper不能在spring mvc 3中工作
如何配置spring mvc 3在json响应中不返回"null"对象?
通过阅读这些和其他来源,我认为实现我想要的最简洁的方法是使用Spring 3.1和可以在mvc-annotation中配置的消息转换器.我更新的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>
Run Code Online (Sandbox Code Playgroud)
服务类与mkyong.com网站上的服务类相同,只是我注释了商店名称变量的设置,所以它是null,即
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) …Run Code Online (Sandbox Code Playgroud) 想象一下,我在Spring 3 @Controller中有这个带注释的方法
@RequestMapping("")
public @ResponseBody MyObject index(@RequestBody OtherObject obj) {
MyObject result = ...;
return result;
}
Run Code Online (Sandbox Code Playgroud)
但我需要配置输出json格式,就像我在做:
ObjectMapper om = new ObjectMapper();
om.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
om.getSerializationConfig()
.setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT);
om.getSerializationConfig()
.set(SerializationConfig.Feature.INDENT_OUTPUT, false);
Run Code Online (Sandbox Code Playgroud)
有没有办法配置这种行为?
我发现了几个相关的问题,但我不确定如何使它们适应我的具体情况:
谢谢 !
我的目标是将spring mvc 3配置为不在json响应中返回"null"对象.我问过如何配置spring mvc 3以不在json响应中返回"null"对象?.我得到的建议是配置ObjectMapper,将序列化包含设置为JsonSerialize.Inclusion.NON_NULL.所以基于Spring配置@ResponseBody JSON格式,我在spring配置文件中做了以下更改.但是在应用启动期间我收到错误"拒绝的bean名称'jacksonObjectMapper':没有识别出URL路径org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:86-AbstractDetectingUrlHandlerMapping.java".
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<!--<mvc:view-controller path="/" view-name="welcome"/>-->
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean …Run Code Online (Sandbox Code Playgroud)