Spring boot can I replace ResourceHttpRequestHandler with my own implementation?

I ran into this problem as I have an application that has soothing services, but I need to serve some static ones as well. I also use annotation in this application EnableSwagger2

. It looks like this class is public and I could subclass it, but I'm not sure how to set it up. My goal is to override this line so that I can control over 404s.

+3


source to share


1 answer


So I finally did it and it worked successfully for me. The config got me a little bit, but here it is. Let's say a subclass ResourceHttpRequestHandler

named CustomResourceHandler

. In mine, Application.java

this connected correctly:

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

      



  • This answer helped me with the correct mapper configuration.
  • This Answer about context helped me set the places in my handler correctly. I set them up differently, but it didn't work 100% correctly for all of my resources.
+2


source







All Articles