I have a spring boot application currently in production. It has APIs which return the error in this format
{
"success": true,
"error": "Error message"
}
This error response I'm handling through the controller advisor.
I'm creating v2 APIs and wanted to change the error response structure for the newer APIs. I want to write code in such a way, that It can have two rest advisors one is handling errors for the older APIs, and the other is for v2 APIs. We can have multiple rest advisors in Spring boot and we can set the order of execution using @Order annotation but for my use case, I want to get it executed based on the API path, If it has v2 it should be executed first otherwise it should get skipped.
@RestControllerAdvice(annotations = RestController.class)
@Log4j2
public class V1APIExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public V1ErrorResponse handleNotFoundException(NotFoundException ex) {
return V1ErrorResponse.builder().error(ex.getMessage()).build();
}
}
@RestControllerAdvice(annotations = RestController.class)
@Log4j2
public class V2APIExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public V2ErrorResponse handleNotFoundException(NotFoundException ex) {
return V2ErrorResponse.builder().error(ex.getMessage()).build();
}
}
@GetMapping(value = "v1/user/{id}")
public UserDetails getUser(
@PathVariable("id") String userId
) throws NotFoundException {
return userService.getUserDetails(userId);
}
@GetMapping(value = "v2/user/{id}")
public UserDetails getUser(
@PathVariable("id") String userId
) throws NotFoundException {
return userService.getUserDetails(userId);
}