Pregunta

Tengo actualmente un Resorte de la aplicación web, que utiliza dos controladores, que se extienden AbstractController.Quiero integrar la Primavera de Inicio en la aplicación, por lo que se puede ejecutar como una aplicación independiente.

Estoy frente a un problema, porque la Primavera es no reenviar las llamadas a mi controlador.¿Cómo puedo asignar el controlador a un patrón 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;
    }
}

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

Cuando inicio la aplicación me sale el siguiente mensaje:

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

Pero cuando voy a enviar una solicitud a /índice, recibo mensajes siguientes:

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
¿Fue útil?

Solución

SimpleUrlHandlerMappings están ordenados y, como se describe en el javadoc el valor predeterminado es Integer.MAX_VALUE lo que significa que tienen la prioridad más baja posible.Esto hace que ResourceHttpRequestHandler (que se asigna a /** y tiene una orden de Integer.MAX_VALUE - 1 de forma predeterminada) para tomar precedencia sobre la asignación de su controlador.

Actualización de su sampleServletMapping() método para establecer la asignación del orden de un valor menor que Integer.MAX_VALUE - 1.Por ejemplo:

@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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top