mardi 31 mars 2015

Spring MVC 1 to 1 mapping of a URL to .HTML without exposing path

I am using Spring 4.1.5 with Boot 1.2 on a webservice that does not serve up any JSPs. I don't want to add a JSP servlet but I want it to serve up a single canary page that shows in a prettier html type format the information that would be provided at the /manage/health endpoint.


I have a file in webapp/canary/canary.html I want to serve this up from the url: www.mywebservice.com:9343/canary, exactly like that, NOT canary.html


I tried doing this:



@Configuration
public class CanaryConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/canary")
.addResourceLocations("/canary/canary.html");
}
}


That doesn't work however.


It is expecting the handler to provide a file name. So in otherwords the location should be something like: /canary/


and the handler would something like: /canary/**


With that, the URL www.mywebservice.com:9343/canary/canary.html would work like a charm.


HOWEVER, I want the URL to resolve www.mywebservice.com:9343/canary to webapp/canary/canary.html without me having to type the html.


This is really easy in a jsp servlet because you can set the suffix ect...


I looked at ResourceResolver but it didn't make sense to me how I would link that into my current configuration.


It looks like what I want:



Provides mechanisms for resolving an incoming request to an actual Resource and for obtaining the public URL path that clients should use when requesting the resource.



See: ResourceResolver Documentation


Any help would be very beneficial.


Also I am very aware that I can put html in the resources/static and several other places that are automatically configured. That always requires the .html to be typed, which is not what I want in this case so that won't work. Thanks!


Aucun commentaire:

Enregistrer un commentaire