如何在 Spring 引导中从端点响应中全局省略空 xml 标记?
How to globally omit empty xml tag from endpoint responses in Spring Boot?
在我的应用程序中,我需要同时支持 json 和 xml 响应格式,并且希望从响应中省略空值。在 json 响应中这工作正常,但在 xml 响应中则不行。结果:
{
"anotherValue": "value"
}
和:
<MyDoc>
<value/>
<anotherValue>value</anotherValue>
</MyDoc>
我希望 xml 看起来像:
<MyDoc>
<anotherValue>value</anotherValue>
</MyDoc>
用 @JsonInclude(JsonInclude.Include.NON_NULL)
注释每个响应 class 是一个可能的解决方案,但我希望它在全局配置。我的代码:
@SpringBootApplication
@RestController
@Configuration
public class Application {
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
return builder
.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.build();
}
@GetMapping(value = "api/json", produces = APPLICATION_JSON_VALUE)
public MyDoc json() {
return new MyDoc(null, "value");
}
@GetMapping(value = "api/xml", produces = APPLICATION_XML_VALUE)
public MyDoc xml() {
return new MyDoc(null, "value");
}
public static class MyDoc {
public String value;
public String anotherValue;
public MyDoc(String val1, String val2) {
this.value = val1;
this.anotherValue = val2;
}
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我在这里错过了什么?任何帮助表示赞赏!谢谢,
达格
这对我来说很好...
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
};
}
注意输出XML你需要使用jackson-dataformat-xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
在我的应用程序中,我需要同时支持 json 和 xml 响应格式,并且希望从响应中省略空值。在 json 响应中这工作正常,但在 xml 响应中则不行。结果:
{
"anotherValue": "value"
}
和:
<MyDoc>
<value/>
<anotherValue>value</anotherValue>
</MyDoc>
我希望 xml 看起来像:
<MyDoc>
<anotherValue>value</anotherValue>
</MyDoc>
用 @JsonInclude(JsonInclude.Include.NON_NULL)
注释每个响应 class 是一个可能的解决方案,但我希望它在全局配置。我的代码:
@SpringBootApplication
@RestController
@Configuration
public class Application {
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
return builder
.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.build();
}
@GetMapping(value = "api/json", produces = APPLICATION_JSON_VALUE)
public MyDoc json() {
return new MyDoc(null, "value");
}
@GetMapping(value = "api/xml", produces = APPLICATION_XML_VALUE)
public MyDoc xml() {
return new MyDoc(null, "value");
}
public static class MyDoc {
public String value;
public String anotherValue;
public MyDoc(String val1, String val2) {
this.value = val1;
this.anotherValue = val2;
}
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我在这里错过了什么?任何帮助表示赞赏!谢谢, 达格
这对我来说很好...
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
};
}
注意输出XML你需要使用jackson-dataformat-xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>