如何解决Micronaut-超过1条路线与传入请求匹配
我在测试控制器时遇到问题,但无法在简单的应用程序中重现该问题。当我运行该应用程序时,它运行正常。
我有一个控制器:
public interface HomeOperations {
@Post // Micronaut's Post
HttpResponse<TheResponse> create(@NotNull @Valid @Body TheCommand command);
}
@Controller("/home")
@Secured("user")
@RequiredArgsConstructor // lombok
@Validated
public class HomeController implements HomeOperations {
@Override
@SneakyThrows // lombok
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<TheResponse> create(@NotNull @Valid @Body TheCommand command) {
return HttpResponse.ok(...);
}
}
请注意,@Post
注释位于界面中。
该控制器的问题是路由POST /home
未匹配。我发现问题出在AnnotatedMethodRouteBuilder#process(BeanDefinition<?> definition,ExecutableMethod<?,?> method)
--- method
中没有HTTP映射(即最终是@Post
)。
这是测试失败的输出:
12:51:39.828 [nioEventLoopGroup-1-4] DEBUG DefaultTokenResolver - Request POST,/home,no token found.
12:51:39.833 [nioEventLoopGroup-1-4] DEBUG Authenticator - com.example.controllers.DummyAuthenticationProvider
12:51:39.835 [nioEventLoopGroup-1-4] INFO DummyAuthenticationProvider - Successful dummy login [user]
12:51:39.904 [nioEventLoopGroup-1-4] DEBUG SecurityFilter - Attributes: roles=>[user],username=>880f1a24-4997-40a6-bbf1-dde1f3871451
12:51:39.905 [nioEventLoopGroup-1-4] DEBUG InterceptUrlMapRule - One or more of the IP patterns matched the host address [127.0.0.1]. Continuing request processing.
12:51:39.908 [nioEventLoopGroup-1-4] DEBUG InterceptUrlMapRule - No url map pattern exact match found for path [/home] and method [POST]. Searching in patterns with no defined method.
12:51:39.909 [nioEventLoopGroup-1-4] DEBUG InterceptUrlMapRule - No url map pattern match found for path [/home]. Returning unknown.
12:51:39.910 [nioEventLoopGroup-1-4] DEBUG SecurityFilter - Authorized request POST /home. No rule provider authorized or rejected the request.
ControllerServerTest > testRegisterEntity() FAILED
io.micronaut.http.client.exceptions.HttpClientResponseException: Forbidden
因此,我创建了此测试:
// Receives HomeController.class as parameter
void testController(final Class<?> type) {
final BeanDefinition<?> definition =
this.context.getBeanDefinition(type);
Assertions.assertNotNull(
definition.getAnnotation(Controller.class),"Controller annotation is null"
);
definition.getExecutableMethods()
.forEach(method -> {
if (!method.hasStereotype(Executable.class)) {
return;
}
// These two are failing:
final Optional<Class<? extends Annotation>> http =
method.getAnnotationTypeByStereotype(HttpMethodMapping.class);
final Optional<Class<? extends Annotation>> uri =
method.getAnnotationTypeByStereotype(UriMapping.class);
Assertions.assertTrue(
http.isPresent(),String.format("Missing method mapping for method %s",method)
);
Assertions.assertTrue(
uri.isPresent(),String.format("Missing URI mapping for method %s",method)
);
});
}
方法create
既没有HttpMethodMapping
也没有UriMapping
。
如果我将@Post
注释从界面移动到控制器类,则此测试通过,但是将请求发送到POST /home
的其他测试失败,并显示以下消息:
More than 1 route matched the incoming request. The following routes matched /home: POST - /home,POST - /home
io.micronaut.http.client.exceptions.HttpClientResponseException: More than 1 route matched the incoming request. The following routes matched /home: POST - /home,POST - /home
这是我的测试配置:
micronaut:
server:
port: -1
security:
basic-auth:
enabled: true
token:
basic-auth:
enabled: false
jwt:
enabled: false
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。