N
N
Nikolai Stupak2016-06-23 09:40:49
Java
Nikolai Stupak, 2016-06-23 09:40:49

How to configure the mapping of requests for web application resources?

The web application has the following controller:

@RestController
@RequestMapping("/")
public class HttpRequestController implements ControllerConstants {

    @Autowired
    private ApplicationContext context;
    
    @Autowired
    private ApiService apiService;

    @RequestMapping(value = "/{version}/**", method = {GET, POST, PUT, DELETE})
    public Object handleHttpRequest(@PathVariable("version") String version, HttpServletRequest request) {
        final Api api = apiService.getApiByVersion(version);
        if ( api == null ) {
            throw new NotFoundException("Api with version " + version + " not founded");
        }
        //далее вызов методов api
    }

    @RequestMapping("/example/{example}.html")
    public ModelAndView getExample(@PathVariable("example") String example) {
        return new ModelAndView("example/" + example);
    }
    
}

And this config:
@Configuration
@EnableWebMvc
@ComponentScan()
public class SpringWebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public InternalResourceViewResolver getInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".html");
    return resolver;
  }

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }

  @Bean
  public WebContentInterceptor webContentInterceptor() {
    WebContentInterceptor interceptor = new WebContentInterceptor();
    interceptor.setCacheSeconds(0);
    interceptor.setUseExpiresHeader(true);
    interceptor.setUseCacheControlHeader(true);
    interceptor.setUseCacheControlNoStore(true);

    return interceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(webContentInterceptor());
  }
}

In the WEB-INF directory, there is an example directory , which contains the some.html file .
The application is deployed to Tomcat with the context path /api .
When I send a request to localhost:8080/api/v1/someMethod , the call is processed correctly, the method is called and the result is sent to the client. When I send a request to localhost:8080/api/example/some.html , I get the following error in response:
b923a06dbfbb48b1bc9b423aefa2f278.png.
Can you tell me how to make requests like localhost:8080/api/example/some.html return the requested page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2016-06-23
@jaxtr

That's right, because example/some.html falls under both rules.
The /{version}/** rule in your case overrides all options, so the easiest way to get rid of the problem is to make the rule more restrictive (for example, /api/{version}/**).
And yes, in @RestController to return ModelAndView from methods, IMHO, somehow ugly.
UPD: at random, I came up with this option:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**.html").addResourceLocations("/WEB-INF/");
    }

Adding this to SpringWebConfig will cause all files ending in .html to be looked up in /WEB-INF/. You can remove the processing of .html requests from the controller

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question