我有以下控制器:
@RestController
@RequestMapping("/api/{brand}"
public class CarController {
@GetMapping
public List<Car> getCars(@PathVariable("brand") String brand) {
// Some implementation
}
@GetMapping("/{model}")
public Car getCar(@PathVariable("model") String model) {
// Some implementation
}
@PostMapping("/{model}")
public Car addCar(@PathVariable("model") String model), @RequestBody Car car) {
// Some implementation
}
}
以及以下内容RestControllerAdvice
:
@RestControllerAdvice(assignableTypes = {CarController.class})
public class InterceptModelPathParameterControllerAdvice {
@Autowired
CarService carService;
@ModelAttribute
public void validateModel(@PathVariable("model") String model) {
if (!carService.isSupportedModel(model)) throw new RuntimeException("This model is not supprted by this application.");
}
}
validateModel
正确验证getCar
and方法,addCar
但它也验证getCars
方法。该getCars
方法没有{model}
@PathVariable
,因此对该端点的请求将始终导致RuntimeException
.
有什么方法可以排除方法受到 a ControllerAdvice
andModelAttribute
组合的影响?