spring 注解 @ResponseBody 返回JSON 能不能设置他不返回为 null 的值


spring 注解 @ResponseBody 返回 JSON 能不能设置他不返回为 null 的值


 java


 @RequestMapping(value="/{username}",method=RequestMethod.GET,params="json")
    @ResponseBody
    public User show(@PathVariable String username) {
        return users.get(username);
    }

spring java json

披羊皮的狼 11 years, 5 months ago

设置 jackson 忽略 null


 <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="serializationInclusion">
                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

消逝D存在 answered 11 years, 5 months ago

可以在页面显示的时候使用三目,读取如果是null就输出空字符

PowgeE answered 11 years, 5 months ago

Your Answer