java - Spring REST服务: how to configure to remove null objects in json response

标签 java spring jackson

我有一个返回 json 响应的 Spring Web 服务。我使用此处给出的示例来创建服务: 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

How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?

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

通过阅读这些和其他来源,我发现实现我想要的最干净的方法是使用 Spring 3.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 jar 是 jackson-mapper-asl 1.9.0 和 jackson-core-asl 1.9.0。这些是我添加到 pom 中的唯一新 jar,作为我从 mkyong.com 下载的 spring-json 项目的一部分提供。

项目构建成功,但是当我通过浏览器调用服务时,我仍然得到相同的结果,即 {"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 注释让它工作,但这个解决方案不适合我。

我不明白为什么它在代码中工作但在配置中不起作用,所以任何帮助都会很棒。

最佳答案

从 Jackson 2.0 开始,您可以使用 JsonInclude

@JsonInclude(Include.NON_NULL)
public class Shop {
    //...
}

关于java - Spring REST服务: how to configure to remove null objects in json response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44929389/

相关文章:

java - 两次扩展 Java 类

mysql - bluemix 服务器在 MySQL 中给出 SQLGrammarException

spring - REDIS 桌面管理器错误

java - 在 Java 中重新格式化 JSON 数组的最有效内存方式

java - 无法在Cloudera VM中使用java(在Eclipse中)连接到hbase

Java Files.getFileAttributeView 创建时间戳精度不是 ext4 上的毫秒

java - 泽西 jdbc @resource 不工作

java - 我应该使用什么设计模式?使用 Spring 框架

kotlin - 为什么 Swagger 不检测可选的 JSON 属性?

java - Spring Boot从资源中的json文件读取然后返回完全相同的数据而不操作数据