简体   繁体   English

SPRING REST:以JSON格式从响应中删除空对象

[英]SPRING REST: Removing empty objects from response in JSON format

I don't have option of spring.xml so i went by annotated method. 我没有spring.xml的选项,所以我采用了带注释的方法。

I have below REST Interfaces in package : com.dpk.cm.impl.ecommerce.rest and implementation in com.dpk.cm.impl.ecommerce.rest.services 我在com.dpk.cm.impl.ecommerce.rest包中有以下REST接口:在com.dpk.cm.impl.ecommerce.rest.services中有实现

I created one spring config class: but seems like i am still seeing in my JSON response empty objects. 我创建了一个spring config类:但是似乎我仍然在JSON响应中看到空对象。

Below is my code : 下面是我的代码:

@Configuration
@ComponentScan(basePackages = "com.dpk.cm.impl.ecommerce.rest")
@EnableWebMvc
public class SpringConfig extends WebMvcConfigurerAdapter {


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        final ObjectMapper objectMapper = new ObjectMapper();
       // objectMapper.setSerializationInclusion(Inclusion.NON_EMPTY);
        objectMapper.setSerializationInclusion(Include.NON_EMPTY);    
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
    }
}

How to remove the Empty Objects from the JSON Reponse Object. 如何从JSON响应对象中删除空对象。

I had similar requirement, but though I use CXF framework on spring boot, there spring boot was creating ObjectMapper which was overriding configuration. 我也有类似的要求,但是尽管我在春季启动时使用了CXF框架,但春季启动时正在创建ObjectMapper,该对象覆盖了配置。 Hence I was manually create ObjectMapper as shown below. 因此,我手动创建了ObjectMapper,如下所示。

@Bean(name = "objectMapper")
    public ObjectMapper getObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(
                SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
                true);
        mapper.setSerializationInclusion(Include.NON_NULL);
        return mapper;
    }

You can create your custom serializer where you can add a condition on serialization of the object. 您可以创建自定义序列化器,在其中可以添加对象序列化的条件。

Model 模型

@JsonSerialize(using = IgnoreEmptyPersonSerializer.class)
public class Person {

    private String name;
    private String address;

    public Person(String name, String address){
        this.name = name;
        this.address = address;
    }
    ...
    //setters and getters
    ...
}

Custom Serializer 自定义序列化器

public class IgnoreEmptyPersonSerializer extends JsonSerializer<Person> {

    @Override
    public void serialize(Person value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String name = value.getName();
        String address = value.getAddress();

        //Dont serialize it if it is empty
        if((name == null || name.trim().equals("")) &&
                (address == null || address.trim().equals(""))){
            return;
        }

        jgen.writeStartObject();
        jgen.writeFieldName("name");
        jgen.writeString(value.getName());
        jgen.writeFieldName("address");
        jgen.writeString(value.getAddress());
        jgen.writeEndObject();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从JSON Response(Spring REST)中删除具有空对象的空对象和空对象 - Removing null and empty objects from the JSON Response(Spring REST) with empty object inside empty object Spring Rest数据如何更改默认格式,从json响应中删除“_”下划线 - Spring Rest Data how to change default format removing “_” underscore from json response 从 JSON 响应中移除非 null 对象 - Removing not null objects from JSON response Spring MVC中REST响应对象的部分JSON序列化 - Partial JSON serialization of REST response objects in Spring MVC Spring REST Service:如何配置为在JSON响应中删除空对象 - Spring REST Service: how to configure to remove null objects in json response Spring rest 调用 JSON 来自 Z0ECD11C1D7A287401D148A23BBDZ2F 发生器的响应大小 - Spring rest call JSON response size from JSON generator REST API 响应为 XML 格式,使用 Resttemplate 从 Spring Boot 调用 - Rest API response as XML format, calling from Spring Boot with Resttemplate 从 json 响应中排除空对象、空数组和空对象数组 - Exclude empty objects, empty arrays and arrays of empty objects from json response Spring Rest API控制器的意外JSon响应 - Unexpected JSon response from Spring Rest API Controller 如何包装来自 Spring REST 存储库的 JSON 响应? - How to wrap JSON response from Spring REST repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM