Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java configuration of SimpleUrlHandlerMapping (Spring boot)

I have an existing Spring web application, that uses two controllers, which extend AbstractController. I want to integrate Spring Boot into the application, so that we can run it as a standalone application.

I am facing a problem, because Spring is not forwarding the calls to my controller. How can I map the controller to an URL pattern like "/app/*"?

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

When I start the application I get the following message:

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

But when I send a request to /index, I get following messages:

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
like image 869
lmazgon Avatar asked Jul 30 '14 12:07

lmazgon


1 Answers

SimpleUrlHandlerMappings are ordered and, as described in the javadoc the default is Integer.MAX_VALUE which means that they have the lowest possible precedence. This causes ResourceHttpRequestHandler (which is mapped to /** and has an order of Integer.MAX_VALUE - 1 by default) to take precedence over the mapping for your controller.

Update your sampleServletMapping() method to set your mapping's order to a value that's less than Integer.MAX_VALUE - 1. For example:

@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;
}
like image 181
Andy Wilkinson Avatar answered Dec 09 '22 17:12

Andy Wilkinson