简体   繁体   English

如何在 Spring Boot 2 中定义默认处理程序

[英]How to define a default handler in Spring Boot 2

How can I define a default handler, or controller action, that will be invoked when no other handler is found for a given URL?如何定义默认处理程序或控制器操作,当找不到给定 URL 的其他处理程序时将调用该处理程序?

I tried by using a catch-all pattern /** (syntax is Kotlin):我尝试使用一个包罗万象的模式/** (语法是 Kotlin):

@Controller
class DefaultController {

    @RequestMapping("/**")
    fun default(...) {
        ...
    }
}

But this gets matched with higher precedence that Spring's own handlers, for example the static file path configured in spring.mvc.static-path-pattern is no longer available.但这与Spring 自己的处理程序的更高优先级相匹配例如spring.mvc.static-path-pattern配置的静态文件路径不再可用。 I need my default handler to have the lowest precedence.我需要我的默认处理程序具有最低优先级。

You would have to add your implementation of HandlerMapping and add it to list of handlers.您必须添加HandlerMapping的实现并将其添加到处理程序列表中。 You need to specify the order of handlers taking care of request as well:您还需要指定处理请求的处理程序的顺序:

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
    SimpleUrlHandlerMapping simpleUrlHandlerMapping
      = new SimpleUrlHandlerMapping();
    Map<String, Object> urlMap = new HashMap<>();
    urlMap.put("/**", defaultController());
    simpleUrlHandlerMapping.setUrlMap(urlMap);
    simpleUrlHandlerMapping.setOrder(1);
    return simpleUrlHandlerMapping;
}

Here the defaultController() method returns a @Controller , that you have defined for the given mapping, so DefaultController .这里defaultController()方法返回一个@Controller ,它是您为给定映射定义的,所以DefaultController The setOrder method defines a priority (order) of handlers, starting from 0. Of course some default HandlerMapping must be defined as a @Bean as well. setOrder方法定义了处理程序的优先级(顺序),从 0 开始。当然一些默认的HandlerMapping必须定义为@Bean More about such configuration you can find here .您可以在此处找到有关此类配置的更多信息。

Edit with some thoughts from @Tobia:使用@Tobia 的一些想法进行编辑:

You need to remove the @RequestMapping annotation so that the controller is not picked up by RequestMappingHandlerMapping and implement the AbstractController interface to override the definition of controller in handleRequestInternal() .您需要删除@RequestMapping注释,以便RequestMappingHandlerMapping不会选择控制器并实现AbstractController接口以覆盖handleRequestInternal()的控制器定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot,默认身份验证失败处理程序如何工作 - spring boot, how does default authentication failure handler work 如何为默认的 Spring Boot 2 指标定义附加或自定义标签? - How to define additional or custom tags for default Spring Boot 2 metrics? 如何在 Spring Boot 中定义自定义对象映射器而不影响 Spring Boot 的默认对象映射器? - How to Define a Custom Object Mapper In Spring Boot Without Impacting Default Object Mapper of Spring Boot? 如何设置默认 HTTP 响应头并在 Spring 引导 Rest Z9BBF37797BF82CFEBA263 中的异常处理程序中覆盖它们 - How to set default HTTP response headers and override them in the exception handler in Spring Boot Rest Controller 如何测试 Spring 引导处理程序拦截器 - How to test a Spring Boot handler Interceptor 如何在Spring Boot中为嵌入式Jetty设置Handler? - How to set Handler for embedded Jetty in Spring Boot? Spring Boot如何测试Netty Handler? - Spring Boot how to test Netty Handler? 如何在spring-boot中定义依赖项注入? - How define the dependency injections in spring-boot? spring boot - 如何正确定义模板位置? - spring boot - how to correctly define template locations? 如何在 Spring Boot 中定义来自地址的邮件? - How to define mail from address in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM