Reactive Spring 不支持 ServerHttpRequest 作为 REST 端点测试中的参数?

Posted

技术标签:

【中文标题】Reactive Spring 不支持 ServerHttpRequest 作为 REST 端点测试中的参数?【英文标题】:Reactive Spring does not support ServerHttpRequest as parameter in REST endpoint tests? 【发布时间】:2019-04-23 06:06:44 【问题描述】:

问题与this one 非常相似。除了我使用的事实:

    org.springframework.http.server.ServerHttpRequest 不是 HttpServletRequest。 测试代码中出现异常。真实通话有效。

代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SecurityTests.SecurityTestsApplication.class)
@TestPropertySource(properties = "")
@AutoConfigureWebTestClient
public class SecurityTests 
        @Test 
        public void myTest()  
            //send request to myUrl and got 500 
        


@RestController
@RequestMapping("/myPath")
public class MyController 
    @PostMapping
    public Mono<Void> myMethod(ServerHttpRequest request) 
        return Mono.empty()
    


例外是:

java.lang.IllegalStateException: Failed to resolve argument 1 of type 'org.springframework.http.server.ServerHttpRequest' on public reactor.core.publisher.Mono<java.lang.Void> MyController$MockitoMock$606550817.myMethod(org.springframework.http.server.ServerHttpRequest)
        at org.springframework.web.reactive.result.method.InvocableHandlerMethod.getArgumentError(InvocableHandlerMethod.java:228) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArg(InvocableHandlerMethod.java:223) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$null$1(InvocableHandlerMethod.java:179) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_131]
        at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$resolveArguments$2(InvocableHandlerMethod.java:177) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_131]
        at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_131]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_131]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_131]
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_131]
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_131]
        at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_131]
        at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArguments(InvocableHandlerMethod.java:183) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        ...
Caused by: java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.http.server.ServerHttpRequest
    at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.createAttribute(ModelAttributeMethodArgumentResolver.java:213) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.prepareAttributeMono(ModelAttributeMethodArgumentResolver.java:163) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.resolveArgument(ModelAttributeMethodArgumentResolver.java:117) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.reactive.result.method.InvocableHandlerMethod.resolveArg(InvocableHandlerMethod.java:214) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 227 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.springframework.http.server.ServerHttpRequest.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_131]
    at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_131]
    at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.createAttribute(ModelAttributeMethodArgumentResolver.java:210) ~[spring-webflux-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 230 common frames omitted

【问题讨论】:

【参考方案1】:

你导入了错误的类:

org.springframework.http.server.ServerHttpRequest 用于 Spring MVC org.springframework.http.server.reactive.ServerHttpRequest 用于 Spring WebFlux

【讨论】:

以上是关于Reactive Spring 不支持 ServerHttpRequest 作为 REST 端点测试中的参数?的主要内容,如果未能解决你的问题,请参考以下文章

Reactive Spring 不支持 ServerHttpRequest 作为 REST 端点测试中的参数?

Spring WebFlux Reactive 和 Kotlin Coroutines 启动错误

Spring容器类型推断

Spring WebClient 放置映射:不支持内容类型“application/json”

WebFlux+Spring Data Reactive,从头到脚构建一个响应式的微服务

使用 Spring Reactive 时如何验证 Mono