
这是一个解决方案。
给定
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface UseAdviceA { public String myValue();}@RestController@UseAdviceA(myValue = "ApiController")@RequestMapping("/myapi")class ApiController { ...}您的控制器建议应类似于
@ControllerAdvice(annotations = {UseAdviceA.class})class AdviceA { @ExceptionHandler({SomeException.class}) public ResponseEntity<String> handleSomeException(SomeException pe, HandlerMethod handlerMethod) { String value = handlerMethod.getMethod().getDeclaringClass().getAnnotation(UseAdviceA.class).myValue(); //value will be ApiController return new ResponseEntity<>("SomeString", HttpStatus.BAD_REQUEST); }欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)