1

Spring Boot with Spring Data Rest - how to use a custom error handler. Created an error controller I tried to skip the default error handler by using following code. Why it is not working!

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration(exclude = { BasicErrorController.class })
@EnableMetrics

public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        .....................
        .....................

and error controller as below

@Component
@RestController
@RequestMapping(value = "/error")
public class CustomErrorController extends BasicErrorController {

    public CustomErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
        // TODO Auto-generated constructor stub
    }

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String error() {
        return "Error handling";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}
Tech Gunu
  • 11
  • 2

2 Answers2

0

I haven't used this kind of solution, but, it seems that your request mapping is not right.

The request mapping of CustomErrorController is '/error', and in

@RequestMapping(value = PATH)
public String error() {
    return "Error handling";
}

There is a another '/error' in request mapping path. Then the url for this error handler is '/error/error'.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
0

You have @RequestMapping("/error") annotation on your controller and second @RequestMapping("/error") on your method. This results in /error/error mapping, not the /error mapping as you specified in getErrorPath() method and maybe in your configuration (application.properties, server.path.error).

happy_marmoset
  • 2,137
  • 3
  • 20
  • 25