I Have a Spring Boot application which has the org.springframework.web.servlet.i18n.CookieLocaleResolver
for locale resolver. If there is a invalid language cookie like !en
then there will be an exception java.lang.IllegalArgumentException: Locale part "!en" contains invalid characters
.
The problem is this exception is not handled by Spring Boot instead it is forwarded to Servlet container. So the default error page of the the container is shown (In my case it is JBoss EAP 6) which will show the stacktrace.
Other exceptions from the controllers are handled properly. For example I have a controller mapping which will throw / by zero error
which is handled properly.
I have tried error page configuration in web.xml as follows.
<error-page>
<location>/500</location>
</error-page>
And mapped both /error
and /500
to a MVC controller as follows.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class CustomErrorController extends AbstractErrorController {
public static final String ERROR_500 = "/500";
private static final String ERROR_PATH= "/error";
@Autowired
public CustomErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes);
}
/**
* Responsible for handling all errors and throw especial exceptions
* for some HTTP status codes. Otherwise, it will return a map that
* ultimately will be converted to a json error.
*/
@RequestMapping({ERROR_PATH,ERROR_500})
public ResponseEntity<?> handleErrors(HttpServletRequest request) {
return ResponseEntity.status(getStatus(request)).body(getErrorAttributes(request, false));
}
@Override
public String getErrorPath() {
return ERROR_PATH;
}
}
But still I'm getting the container's default error page. How to resolve this.
I think you also need to mention error-code
in the params of your web.xml
like below. It works for me.
<error-page>
<error-code>500</error-code>
<location>/500</location>
</error-page>
and then catch it in the controller
@RequestMapping(value = "/500", method = RequestMethod.GET)
public String error500(Model model) {
//System.out.println("Error 500 ");
return "500"; //will return the 500.jsp
}