Pergunta

Eu tenho um aplicativo Web Spring existente, que usa dois controladores, que estendem o AbstractController.Quero integrar o Spring Boot ao aplicativo, para que possamos executá-lo como um aplicativo independente.

Estou enfrentando um problema, pois o Spring não está encaminhando as chamadas para o meu controlador.Como posso mapear o controlador para um padrão de URL como "/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;
    }
}

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

Quando inicio o aplicativo recebo a seguinte mensagem:

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

Mas quando envio uma solicitação para/index, recebo as seguintes mensagens:

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
Foi útil?

Solução

SimpleUrlHandlerMappings são ordenados e, como descrito no javadoc o padrão é Integer.MAX_VALUE o que significa que eles têm a menor precedência possível.Isso causa ResourceHttpRequestHandler (que está mapeado para /** e tem uma ordem de Integer.MAX_VALUE - 1 por padrão) para ter precedência sobre o mapeamento do seu controlador.

Atualize seu sampleServletMapping() método para definir a ordem do seu mapeamento para um valor menor que Integer.MAX_VALUE - 1.Por exemplo:

@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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top