@ControllerAdvice is an annotation in Spring Framework that is used to handle exceptions globally across multiple controllers in a Spring MVC application.
By using this annotation, you can define methods that will be applied to all controllers or specific controllers to handle exceptions thrown during request processing. These methods can perform actions such as logging the exception, customizing the error response, or redirecting to an error page.
Here’s a simple example of how to use @ControllerAdvice in a Spring MVC application:
@ControllerAdvice
public class GlobalExceptionHandler {
// Exception handling methods go here
}
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
// Handle the exception here
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<String> handleNotFoundException(NotFoundException ex) {
// Handle the NotFoundException here
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found");
}
// Add more exception handling methods as needed
}
实现ResponseBodyAdvice
这个接口来修改返回值,并直接作为 ResponseBody 类型处理器 的返回值。
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.Map;
@ControllerAdvice
public class HelloResponseBodyAdvice implements ResponseBodyAdvice<Map<String, Object>> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Map<String, Object> beforeBodyWrite(Map<String, Object> body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
// 修改返回值
System.out.println("origin map: " + body);
body.put("msg", "hello");
return body;
}
}
@RestController
@RequestMapping("/test")
public class ResponseBodyAdviceController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public Map<String, Object> hello() {
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
return map;
}
}