[英]SpringBoot Webflux cannot return application/xml
In my reactive REST API I'm trying to return an XML
response. 在我的反应性REST API中,我试图返回
XML
响应。 However, I always get a JSON
, namely 406 NOT_ACCEPTABLE
. 但是,我总是得到一个
JSON
,即406 NOT_ACCEPTABLE
。 Any idea why? 知道为什么吗?
@RestController
@RequestMapping(path = "/xml", produces = APPLICATION_XML_VALUE)
public class RestApi {
@GetMapping(path = "/get")
public Publisher<ResponseEntity> get() {
return Mono.just(ResponseEntity.ok().contentType(APPLICATION_XML).body(new Datta("test")));
}
@PostMapping(path = "/post", consumes = APPLICATION_XML_VALUE)
public Publisher<ResponseEntity<Datta>> post(@RequestBody Datta datus) {
datus.setTitle(datus.getTitle() + "!");
return Mono.just(ResponseEntity.ok().contentType(APPLICATION_XML).body(datus));
}
}
java.lang.AssertionError: Expected :application/xml Actual :application/json;charset=UTF-8
java.lang.AssertionError:预期的:application / xml实际的:application / json; charset = UTF-8
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id "io.spring.dependency-management" version "1.0.7.RELEASE"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
These are the links to my REST controller and a unit test . 这些是我的REST 控制器和单元测试的链接。 Thanks!
谢谢!
Apparently, jackson-dataformat-xml
does not yet support XML Marshalling in WebFlux. 显然,
jackson-dataformat-xml
尚不支持WebFlux中的XML编组。 As for now I see two possibilities: 到目前为止,我看到两种可能性:
org.springframework.boot:spring-boot-starter-web
on the classpath (there should be both starter-web and starter-webflux ). org.springframework.boot:spring-boot-starter-web
(应该同时包含starter-web和starter-webflux )。 However, this will only work with Servlet 3.1 runtimes (eg Tomcat). Jaxb2XmlEncoder
and Jaxb2XmlDecoder
): Jaxb2XmlEncoder
和Jaxb2XmlDecoder
): build.gradle : build.gradle :
sourceCompatibility = '11'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// Java 11 removed these Java EE modules
implementation "javax.xml.bind:jaxb-api:2.3.1"
implementation "com.sun.xml.bind:jaxb-core:2.3.0.1"
implementation "com.sun.xml.bind:jaxb-impl:2.3.2"
compileOnly "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
}
POJO : POJO :
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement
public class Datta {
private String title;
}
Mind the 3 javax.xml.bind
dependencies (you don't need these for Java 8
) and the @XmlRootElement
annotation. 注意3个
javax.xml.bind
依赖项(对于Java 8
不需要这些依赖项)和@XmlRootElement
批注。 This solution works right away, however if you want further customization, implement your own WebFluxConfigurer
: 该解决方案可以立即
WebFluxConfigurer
,但是,如果您需要进一步的自定义,请实现自己的WebFluxConfigurer
:
@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.registerDefaults(false);
configurer.customCodecs().decoder(new Jaxb2XmlDecoder()); // <- here
configurer.customCodecs().encoder(new Jaxb2XmlEncoder()); // <- here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.