I am running into this issue since I have an app that has restful services but I need to serve up some static ones too. In this app I also use the EnableSwagger2
annotation. It looks like this class is public and I could subclass it but I am not sure how to configure that. My goal is to override this line so I can have control over 404s.
So I finally did do this and it worked successfully for me. The config got me for a bit but here it is. Say you subclass ResourceHttpRequestHandler
with one named CustomResourceHandler
. In my Application.java
this wired it up properly:
@Bean
public ResourceHttpRequestHandler resourceHttpRequestHandler() {
ResourceHttpRequestHandler requestHandler = new CustomResourceHandler();
requestHandler.setLocations(Arrays.<Resource>asList(applicationContext.getResource("/")));
return requestHandler;
}
@Bean
public SimpleUrlHandlerMapping sampleServletMapping(){
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MAX_VALUE - 2);
Properties urlProperties = new Properties();
urlProperties.put("/**", "resourceHttpRequestHandler");
mapping.setMappings(urlProperties);
return mapping;
}
@Autowired
ApplicationContext applicationContext;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With