Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Disable /error mapping

I am creating an API with Spring Boot so wish to disable the /error mapping.

I have set the following props in application.properties:

server.error.whitelabel.enabled=false spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false 

However when I hit /error I get:

HTTP/1.1 500 Internal Server Error Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 03 Aug 2016 15:15:31 GMT Connection: close  {"timestamp":1470237331487,"status":999,"error":"None","message":"No message available"} 

Required Result

HTTP/1.1 404 Internal Server Error Server: Apache-Coyote/1.1 
like image 644
ptimson Avatar asked Aug 03 '16 15:08

ptimson


People also ask

How do I get rid of WhiteLabel error in spring boot?

enabled to false . Another way of disabling the WhiteLabel Error is excluding the ErrorMvcAutoConfiguration . Alternatively, the exclusion can be done in an annotation. When the WhiteLabel Error Page is disabled and no custom error page is provided, the web server's error page (Tomcat, Jetty) is shown.

How do I stop WhiteLabel error?

Disabling the Whitelabel Error PageAdding this entry to the application. properties file will disable the error page and show a concise page that originates from the underlying application container, e.g., Tomcat.

What is spring boot ErrorPageFilter?

Class ErrorPageFilterA Servlet Filter that provides an ErrorPageRegistry for non-embedded applications (i.e. deployed WAR files). It registers error pages and handles application errors by filtering requests and forwarding to the error pages instead of letting the server handle them.

How do you handle exception globally in spring boot?

You can define the @ExceptionHandler method to handle the exceptions as shown. This method should be used for writing the Controller Advice class file. Now, use the code given below to throw the exception from the API. The complete code to handle the exception is given below.


2 Answers

You can disable the ErrorMvcAutoConfiguration :

@SpringBootApplication @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) public class SpringBootLauncher { 

Or through Spring Boot's application.yml/properties:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration 

If this is not an option for you, you may also extend Spring's ErrorController with your own implementation:

@RestController public class MyErrorController implements ErrorController {      private static final String ERROR_MAPPING = "/error";      @RequestMapping(value = ERROR_MAPPING)     public ResponseEntity<String> error() {         return new ResponseEntity<String>(HttpStatus.NOT_FOUND);     }      @Override     public String getErrorPath() {         return ERROR_MAPPING;     } 

Note: Use one of the above techniques (disabled auto-configuration or implement the error-controller). Both together will not work, as mentioned in the comments.

like image 161
alexbt Avatar answered Sep 20 '22 15:09

alexbt


Attributes should be specified via @SpringBootApplication. Example in Kotlin:

@SpringBootApplication(exclude = [ErrorMvcAutoConfiguration::class]) class SpringBootLauncher { 
like image 23
kejm Avatar answered Sep 20 '22 15:09

kejm