简体   繁体   中英

@ControllerAdvice on the method level

I have two ControllerAdvices in my spring mvc project.

//return the json message
@ControllerAdvice(annotations = RestController.class) 
public class AdviceRestController

//redirect to the error
@ControllerAdvice(annotations = Controller.class) 
public class AdviceController

now, I have some @ResponseBody annotated method in the @Controller annotated class. Is there any annotation for the method level, then I can write the method in my AdviceController for @ResponseBody annotated method only.

ie

//reoponsebody method only
@ReoponsebodyMethod
@ExceptionHandler({Exception.class})
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public String handleIllegalStateException(Exception ex) {
    //return son
}

Depends what you want to do. You can implement a ControllerAdvice annotated class that implements ResponseBodyAdvice<T> .

@ControllerAdvice
public class MyResponseBodyCustomizer implements ResponseBodyAdvice<ResponseBodyObject> {
  @Override
  boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    return false;
  }

  @Override
  ResponseBodyObject beforeBodyWrite(Caravan body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    return null;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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