【问题标题】:Java configuration of SimpleUrlHandlerMapping (Spring boot)SimpleUrlHandlerMapping的Java配置(Spring boot)
【发布时间】:2014-07-30 12:40:41
【问题描述】:

我有一个现有的 Spring Web 应用程序,它使用两个控制器,它们扩展了 AbstractController。我想将 Spring Boot 集成到应用程序中,以便我们可以将其作为独立应用程序运行。

我遇到了一个问题,因为 Spring 没有将调用转发到我的控制器。如何将控制器映射到像“/app/*”这样的 URL 模式?

SampleController.java

@Controller("myController")
public class SampleController extends AbstractController {
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.getWriter().print("Hello world!");
        return null;
    }
}

Application.java

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public SimpleUrlHandlerMapping sampleServletMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();

        Properties urlProperties = new Properties();
        urlProperties.put("/index", "myController");

        mapping.setMappings(urlProperties);

        return mapping;
    }
}

当我启动应用程序时,我收到以下消息:

INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] Mapped URL path [/index] onto handler 'myController'

但是当我向 /index 发送请求时,我收到以下消息:

DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Looking up handler method for path /index
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Did not find handler method for [/index]
DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] Matching patterns for request [/index] are [/**]
DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] URI Template variables for request [/index] are {}
DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] Mapping [/index] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@11195d3e] and 1 interceptor

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    SimpleUrlHandlerMappings 是有序的,因为described in the javadoc 默认为Integer.MAX_VALUE,这意味着它们的优先级最低。这会导致 ResourceHttpRequestHandler(映射到 /** 并且默认情况下具有 Integer.MAX_VALUE - 1 的顺序)优先于控制器的映射。

    更新您的sampleServletMapping() 方法,将映射的顺序设置为小于Integer.MAX_VALUE - 1 的值。例如:

    @Bean
    public SimpleUrlHandlerMapping sampleServletMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(Integer.MAX_VALUE - 2);
    
        Properties urlProperties = new Properties();
        urlProperties.put("/index", "myController");
    
        mapping.setMappings(urlProperties);
    
    
        return mapping;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多