1

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!

njfife
  • 3,555
  • 1
  • 20
  • 31

2 Answers2

1

You can use view controllers to do it. Here is a sample of it. Hope this helps.

    public class AppConfig extends WebMvcConfigurerAdapter {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/canary").setViewName("/canary/canary.html");
        }
}

Note: if you are using tomcat, you might have to configure jsp servlet to server html files.

Related post here.

Community
  • 1
  • 1
minion
  • 4,313
  • 2
  • 20
  • 35
  • So I had already tried that exact thing, but I guess you need to have it be `registry.addViewController("/canary/").setViewName("/canary/canary.html");`, notice the extra / after canary... that was silly but thank you. – njfife Mar 31 '15 at 23:28
  • Also, we are intentionally not using a servlet, this change was done for the purpose of removing the JSP servlet from the webservice. – njfife Mar 31 '15 at 23:30
0

For information sake, the selected answer is the same as the following:

@Controller
public class CanaryController {

    @RequestMapping(value="/canary", method=RequestMethod.GET)
    public String getCanary() {
        return "/canary/canary.html";
    }
}

The above code will work as long as canary(or whatever file/folder) is in your webapp folder.

When I tried this I was trying to set the suffix to .html in my YAML (.yml) file and it wasn't working to I thought that it needed to return to a servlet if it is not a RestController. I was mistaken.

njfife
  • 3,555
  • 1
  • 20
  • 31