如何配置spring mvc 3在json响应中不返回"null"对象?

Bob*_*obo 30 json spring-mvc jackson

json响应的示例如下所示:

{"publicId":"123","status":null,"partner":null,"description":null}
Run Code Online (Sandbox Code Playgroud)

截断响应中的所有空对象会很好.在这种情况下,响应将变为{"publicId":"123"}.

有什么建议?谢谢!

PS:我想我可以在泽西岛做到这一点.另外我相信他们都使用Jackson作为JSON处理器.

后来添加:我的配置:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.SomeCompany.web" />

    <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages/messages" />
        <property name="cacheSeconds" value="0" />
    </bean>

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />
Run Code Online (Sandbox Code Playgroud)

<?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 id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

    <!--<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="atom" value="application/atom+xml"/>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
                <entry key="xml" value="text/xml"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
                    <property name="marshaller">
                        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller" />
                    </property>
                </bean>
            </list>
        </property>
    </bean>
-->
  <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
Run Code Online (Sandbox Code Playgroud)

我的代码:

@Controller
public class SomeController {
    @RequestMapping(value = "/xyz", method = {RequestMethod.GET, RequestMethod.HEAD},
    headers = {"x-requested-with=XMLHttpRequest","Accept=application/json"}, params = "!closed")
    public @ResponseBody
    List<AbcTO> getStuff(
          .......
    }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*int 32

是的,您可以通过使用它们进行注释来为单个类执行此操作,@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)或者您可以通过配置ObjectMapper将序列化包含设置为全面执行此操作JsonSerialize.Inclusion.NON_NULL.

以下是杰克逊常见问题解答中的一些信息:http://wiki.fasterxml.com/JacksonAnnotationSerializeNulls.

注释类很简单,但配置ObjectMapper序列化配置稍微复杂一些.有做后者的一些具体的信息在这里.


Már*_*des 18

不回答这个问题,但这是谷歌的第二个结果.

如果有人来到这里并希望在Spring 4中做到这一点(就像我遇到的那样),你可以使用注释

@JsonInclude(Include.NON_NULL)
Run Code Online (Sandbox Code Playgroud)

在返回班上.

正如评论中所提到的,如果有人感到困惑,那么应该在将转换为JSON的类中使用注释.


Luí*_*res 9

如果使用Spring Boot for REST,您可以在application.properties以下位置执行此操作:

spring.jackson.serialization-inclusion=NON_NULL
Run Code Online (Sandbox Code Playgroud)

资源

  • 我使用spring.jackson.default-property-inclusion = non_absent`,弹簧启动1.5.3 (4认同)