Spring Boot Microservice JSON序列化非空
我目前正在开发一个SpringBoot(版本1.3.1)微服务,它连接到MongoDB后端,并通过控制器向客户端提供后端数据(例如:Provider对象) 该项目有一个扩展ResourceSupport(Ex:ProviderResourceSupport)的类文件,还有一个扩展ResourceSupportAssembler类(Ex:ProviderAssembler)的类,用于生成到响应对象的链接 理想情况下,我的需求是根据需要定制JSON对象,因此使用@JsonView(以下链接-)并在maven项目中添加Spring-Jackson依赖项 我还在application.properties中添加了spring.jackson.serialization inclusion=non-null&spring.jackson.serialization.indent\u output=true 对于控制器中的某个方法,响应将为“ResponseEntitySpring Boot Microservice JSON序列化非空,json,spring,serialization,jackson,Json,Spring,Serialization,Jackson,我目前正在开发一个SpringBoot(版本1.3.1)微服务,它连接到MongoDB后端,并通过控制器向客户端提供后端数据(例如:Provider对象) 该项目有一个扩展ResourceSupport(Ex:ProviderResourceSupport)的类文件,还有一个扩展ResourceSupportAssembler类(Ex:ProviderAssembler)的类,用于生成到响应对象的链接 理想情况下,我的需求是根据需要定制JSON对象,因此使用@JsonView(以下链接-)并在m
- >”,如果数据不存在,此方法将返回“null”响应
我已经在实体对象和控制器上添加了@JsonInclude(Include=NON_NULL),但仍然得到了“NULL”响应
我不希望“空”作为回应,如果有人遇到类似问题,请您帮助我 我修复了从扩展Jackson Mapper Bean的json响应中转义的空属性,但我没有使用Spring Boot,请快速查看并检查这是否适合您
public class Jackson2ObjectMapperCustom extends Jackson2ObjectMapperFactoryBean {
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
getObject().setSerializationInclusion(JsonInclude.Include.NON_NULL).setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.disable(Feature.USE_TRANSIENT_ANNOTATION);
hibernateModule.enable(Feature.FORCE_LAZY_LOADING);
getObject().registerModules(new JavaTimeModule(), hibernateModule);
getObject().configure(SerializationFeature.INDENT_OUTPUT, true);
getObject().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
getObject().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
}
}
在我的例子中,我使用SpringXML配置
<bean id="objectMapper" class="com.xxx.common.Jackson2ObjectMapperCustom" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
如果数据不存在,它应该返回什么?嗨,jny,它应该只返回404的HTTP状态码。我可以检查数据,如果它是空的,我将状态代码填充到404。响应状态代码正确地填充了“404”,但我应该看不到任何数据。相反,我看到的是一个“空”的回应。嗨,非常感谢,谢谢。但我使用的是Spring引导,序列化包含将使用application.properties文件定义。所以我不能使用它,但再次感谢您的帮助。大家好,它现在可以工作了,因为我已将响应类型从“ResponseEntity- >”更改为“ResponseEntity”,并相应地构建响应对象。