【问题标题】:Error Contoller Howto : Spring-boot + Spring-Data-Rest错误控制器如何:Spring-boot + Spring-Data-Rest
【发布时间】:2021-06-15 13:15:00
【问题描述】:

带有 Spring Data Rest 的 Spring Boot - 如何使用自定义错误处理程序。 创建了一个错误控制器,我尝试使用以下代码跳过默认错误处理程序。 为什么它不起作用!

@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);
        .....................
        .....................

和错误控制器如下

@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;
    }
}

【问题讨论】:

    标签: java spring-boot spring-data-rest


    【解决方案1】:

    我没用过这种方案,但是,你的请求映射好像不对。

    CustomErrorController的请求映射是'/error',在

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

    请求映射路径中存在另一个“/错误”。那么这个错误处理程序的 url 是'/error/error'。

    【讨论】:

      【解决方案2】:

      您的控制器上有@RequestMapping("/error") 注释,您的方法上有第二个@RequestMapping("/error")。这会导致/error/error 映射,而不是您在getErrorPath() 方法中指定的/error 映射,并且可能在您的配置中(application.properties、server.path.error)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 2020-11-13
        • 2016-02-07
        • 1970-01-01
        相关资源
        最近更新 更多