问题
I have a spring boot application. trying to send xml payload through postman to a Post request. I get the following exception
java.lang.IllegalStateException: Failed to resolve argument 0 of type 'reactor.core.publisher.Mono' on public reactor.core.publisher.Mono<com.event.gateway.rest.controller.Sir> com.event.gateway.rest.controller.WBController.hello(reactor.core.publisher.Mono<com.event.gateway.rest.controller.Sir>)
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.getArgumentError(InvocableHandlerMethod.java:198) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArg(InvocableHandlerMethod.java:193) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$null$1(InvocableHandlerMethod.java:149) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_91]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$resolveArguments$2(InvocableHandlerMethod.java:147) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_91]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_91]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_91]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_91]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_91]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_91]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_91]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArguments(InvocableHandlerMethod.java:153) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:117) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter.lambda$handle$3(RequestMappingHandlerAdapter.java:312) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at reactor.core.publisher.MonoThenMap$MonoThenApplyMain.onNext(MonoThenMap.java:100) [reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:928) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore$MonoThenIgnoreMain.drain(MonoThenIgnore.java:145) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore.subscribe(MonoThenIgnore.java:54) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenMap.subscribe(MonoThenMap.java:57) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenMap$MonoThenApplyMain.onNext(MonoThenMap.java:130) [reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:68) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:78) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.innerNext(FluxConcatMap.java:263) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxConcatMap$ConcatMapInner.onNext(FluxConcatMap.java:743) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:119) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:928) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore$MonoThenAcceptSubscriber.onNext(MonoThenIgnore.java:261) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:1657) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore$MonoThenAcceptSubscriber.onSubscribe(MonoThenIgnore.java:250) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:59) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore$MonoThenIgnoreMain.drain(MonoThenIgnore.java:151) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore.subscribe(MonoThenIgnore.java:54) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:60) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:402) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:203) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:97) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:57) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:126) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoNext.subscribe(MonoNext.java:41) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoOtherwiseIfEmpty.subscribe(MonoOtherwiseIfEmpty.java:44) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenMap.subscribe(MonoThenMap.java:57) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenMap.subscribe(MonoThenMap.java:57) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoOtherwise.subscribe(MonoOtherwise.java:44) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoOtherwise.subscribe(MonoOtherwise.java:44) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoOtherwise.subscribe(MonoOtherwise.java:44) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore$MonoThenIgnoreMain.drain(MonoThenIgnore.java:169) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoThenIgnore.subscribe(MonoThenIgnore.java:54) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoOtherwise.subscribe(MonoOtherwise.java:44) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.core.publisher.MonoPeekTerminal.subscribe(MonoPeekTerminal.java:62) ~[reactor-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at reactor.ipc.netty.channel.ChannelOperations.applyHandler(ChannelOperations.java:405) ~[reactor-netty-0.6.1.RELEASE.jar:0.6.1.RELEASE]
at reactor.ipc.netty.http.server.HttpServerOperations.onHandlerStart(HttpServerOperations.java:353) ~[reactor-netty-0.6.1.RELEASE.jar:0.6.1.RELEASE]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-all-4.1.8.Final.jar:4.1.8.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) ~[netty-all-4.1.8.Final.jar:4.1.8.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:445) ~[netty-all-4.1.8.Final.jar:4.1.8.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) ~[netty-all-4.1.8.Final.jar:4.1.8.Final]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_91]
Caused by: java.lang.UnsupportedOperationException: null
at org.springframework.core.codec.AbstractDecoder.decodeToMono(AbstractDecoder.java:64) ~[spring-core-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.http.codec.DecoderHttpMessageReader.readMono(DecoderHttpMessageReader.java:84) ~[spring-web-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver.readBody(AbstractMessageReaderArgumentResolver.java:160) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.annotation.RequestBodyArgumentResolver.resolveArgument(RequestBodyArgumentResolver.java:78) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArg(InvocableHandlerMethod.java:184) ~[spring-webflux-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
... 56 common frames omitted
Given below is the POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.event.gateway</groupId>
<artifactId>endpoint-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vu-ms-event-gateway</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Controller
package com.event.gateway.rest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Controller
public class WBController {
@RequestMapping(value ="hello", consumes ="application/xml", produces="application/xml", method=RequestMethod.POST)
@ResponseBody
public Mono <Sir> hello(@RequestBody Mono<Sir> requestBody) {
Flux<String> mono = Mono.just("")
.concatWith(Mono.from(requestBody)
.flatMap(sir -> Flux.fromArray(sir.getLastName().split("")))
.map(String::toUpperCase)
.take(4)
);
mono.subscribe(System.out::println);
return requestBody;
}
}
POJO Class
package com.event.gateway.rest.controller;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonInclude;
import javax.xml.bind.annotation.XmlAccessType;
@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlRootElement(name="sir")
@XmlAccessorType(XmlAccessType.FIELD)
public class Sir implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name ="firstName")
private String firstName;
@XmlElement(name ="lastName")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Sample Postman request
POST /hello HTTP/1.1
Host: localhost:8070
Content-Type: application/xml
Cache-Control: no-cache
Postman-Token: 2132d866-347e-5fcf-de00-2dc8f5f97ac0
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sir>
<firstName>Jane Doe</firstName>
<lastName>Peter</lastName>
</sir>
I get 500 internal server error as response.
Highly appreciate your help.
回答1:
Not sure why but as I can see it uses Jaxb2XmlDecoder
for decoding XML payload and decodeToMono
method is not implemented there.
To fix this you can use Flux<Sir>
type as a requestBody
回答2:
Could you explain what are you trying to achieve in your Controller?
This error probably comes from the fact that you're calling subscribe
in your controller method, effectively consuming the incoming Flux
and returning at the same time.
As a general rule, you shouldn't subscribe to Publisher
s within your Controllers.
回答3:
As pointed out by KOrest, Jaxb2XmlDecoder
did not implement decodeToMono
, but it is now fixed via SPR-16759. So just upgrading to Spring Framework 5.0.6+ / Spring Boot 2.0.2+ should avoid the reported exception.
Side note: like by Brian Clozel, I am not sure of what you try to achieve and you probably should not call subscribe
manually but instead return a transformation of the input. doOnNext
can be used if you want to print the output without triggering the demand artificially.
来源:https://stackoverflow.com/questions/42513185/spring-reactive-xml-payload-exception-java-lang-illegalstateexception-failed-to