Answer the question
In order to leave comments, you need to log in
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);
}
}
@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());
}
}
Answer the question
In order to leave comments, you need to log in
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/");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question