Java 从@RestControllerAdvice中的@ModelAttribute中排除方法

Java 从@RestControllerAdvice中的@ModelAttribute中排除方法,java,spring-boot,spring-mvc,controller-advice,Java,Spring Boot,Spring Mvc,Controller Advice,我有以下控制器: @RestController @请求映射(“/api/{brand}” 公共类汽车控制器{ @GetMapping 公共列表getCars(@PathVariable(“品牌”)字符串品牌){ //一些实现 } @GetMapping(“/{model}”) 公共汽车getCar(@PathVariable(“模型”)字符串模型){ //一些实现 } @后映射(“/{model}”) 公共汽车addCar(@PathVariable(“model”)字符串模型),@Reque

我有以下控制器:

@RestController
@请求映射(“/api/{brand}”
公共类汽车控制器{
@GetMapping
公共列表getCars(@PathVariable(“品牌”)字符串品牌){
//一些实现
}
@GetMapping(“/{model}”)
公共汽车getCar(@PathVariable(“模型”)字符串模型){
//一些实现
}
@后映射(“/{model}”)
公共汽车addCar(@PathVariable(“model”)字符串模型),@RequestBody汽车){
//一些实现
}
}
以及以下
RestControllerAdvice

@RestControllerAdvice(assignableTypes={CarController.class})
公共类侦听器ModelPathParameterControllerAdvice{
@自动连线
汽车服务;
@模型属性
public void validateModel(@PathVariable(“model”)字符串模型){
如果(!carService.isSupportedModel(model))抛出新的RuntimeException(“此应用程序不支持此模型”);
}
}
validateModel
正确验证
getCar
addCar
方法,但它也验证
getCars
方法。
getCars
方法没有
{model}
@PathVariable
,因此对该端点的请求将始终导致
运行时异常


有没有办法排除方法受到
ControllerAdvice
ModelAttribute
组合的影响?

据我所知,在
@ControllerAdvice
中,没有真正的方法排除方法被
@ModelAttribute
拦截。但是,您可以将方法参数从
@PathVariable(“model”)字符串模型更改为
HttpServletRequest请求,并按如下方式更改实现:

@modeldattribute
公共void validateModel(HttpServletRequest){
Map requestAttributes=(Map)request.getAttribute(HandlerMapping.URI\u TEMPLATE\u VARIABLES\u ATTRIBUTE);
if(requestAttributes.containsKey(“模型”){
字符串模型=requestAttributes.get(“模型”);
如果(!carService.isSupportedModel(model))抛出新的RuntimeException(“此应用程序不支持此模型”);
}
}