繁体   English   中英

Spring 引导@PathVariable 验证

[英]Spring boot @PathVariable Validation

在我的 controller 中,我有一个由 GetMapping 注释的方法,我想验证路径变量。 我测试了@max 和@min。 他们还好。 但问题是当我想检查没有传递任何路径变量时,我得到 404 错误代码。 意思是我想像这样测试案例:

http://localhost:9090/api/mancala-game/last-state/

在最后一个 / 之后什么都没有。

我想在我的 ExceptionHandler class 中获取异常。

有任何想法吗? 谢谢你

我认为http://localhost:9090/api/mancala-game/last-state/

http://localhost:9090/api/mancala-game/last-state/{key}是两个 api。

你可以创建/last-state/last-state/{key}

你应该发送路径变量,在 url 中它自己否则你会抛出 404 错误。 当您不发送路径变量时,它将是一个不同的 API:在您的情况下:http://localhost:9090/api/mancala-game/last-state/ 并且此 url 未指向您的 controller 中的任何处理方法,所以你会得到 404 错误。

您要做的是在 REST 端点api/mancala-game/last-state/的末尾添加一个通配符**在您为此 REST 端点调用antMatchers()方法的位置。 例如:

.authorizeRequests().antMatchers(HttpMethod.GET, "api/mancala-game/last-state/**").permitAll() //Other antMatchers after this..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM