I have my Spring MVC application configured to use CORS as such:
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
This works fine for successful requests however when an exception is thrown and is picked up by my error handler, CORS headers are not added.
@ControllerAdvice
public class ApiErrorHandler {
@ExceptionHandler(value = HttpClientErrorException.class)
public ResponseEntity badRequest(Exception ex)
{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorBodyWrapper.wrapErrorInJson(ex.getMessage()));
}
}
Is there a reason CORS does not work for error handlers?
Thanks
Using Spring
CorsFilter
can resolve this problem.I think by default the only method added to the mapping is GET, In your "addCorsMappings" try to specify the methods you want to add the header to
same with you.
and globalException
but Cross-domain calls error by Browser(Chrome etc.)
XMLHttpRequest cannot load hxxp://127.0.0.1:8080/url... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'hxxp://127.0.0.1:3000' is therefore not allowed access.
Trying to add a
@CrossOrigin
annotation on ExceptionHandler still has this errorI am dealing with this now
add a mapping