Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot can I replace ResourceHttpRequestHandler with my own implementation?

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.

like image 478
Barry Avatar asked Apr 06 '17 16:04

Barry


1 Answers

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;
  • This Answer helped me with the proper config of the mapper.
  • This Answer about context helped me get the locations set properly in my handler. I was setting them a different way but it wasn't working 100% properly for all my resources
like image 92
Barry Avatar answered Nov 03 '22 22:11

Barry