Spring Boot Disable /error mapping

前端 未结 3 1300
南笙
南笙 2020-12-28 14:41

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

I have set the following props in application.properties:

s         


        
相关标签:
3条回答
  • 2020-12-28 15:27

    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.

    0 讨论(0)
  • 2020-12-28 15:31

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

    @SpringBootApplication(exclude = [ErrorMvcAutoConfiguration::class])
    class SpringBootLauncher {
    
    0 讨论(0)
  • 2020-12-28 15:37

    In my case the problem was with web resources referenced in the header of the login page. Specifically, css was referenced in the header, but did not actually exist in the project.

    What might also be helpful, in my WebSecurityConfigurerAdapter implementation I commented out the body of configure(WebSecurity web) first, then on trying to login, instead of displaying the above error json my browser's address bar would display the url to the resource causing the problem.

    0 讨论(0)
提交回复
热议问题