0

I am building a simple REST service to fetch some data and then show it. Very basic so far. I am using a jersey/tomcat combination, build with Maven.

Everything goes well, except for the fact that I want to add some css and perhaps in a later stadium jquery to the webpages. Because of the simplicity, I have coded my resource like this:

@Path("/home")
public class Home {

    @GET
    @Produces("text/html")
    public String getStatus(){
        String html = "<!DOCTYPE html><html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\"> </head><body><h1>status!</h1>normal text</body></html>";
        return html;
    }
}

This is a very simple example, but I have no clue as to where I should put the 'style.css' file.

I have been searching the web, and some people suggest that I should create a webapp folder and put the files in there, but not under the WEB-INF folder (which will automatically be put in the webapp folder). But that doesn't really help. I created the folder by adding this in my POM:

<resource>
       <directory>${basedir}/webapp</directory>
</resource>

Other suggestions were mainly directed at using .jsp files, but I dont want to use those. I want to keep it as simple and straightforward as possible.

So basically the question is: how do I enable CSS to that kind of jersey response?

On a sidenote: The only reason I want to return hardcoded html the way I do now, is because it is simple. But I find it an ugly way. Is there a more flexible way to do this without the use of .jsp files?

anothernoc
  • 177
  • 1
  • 2
  • 10
  • 2
    Your html, js and css should not be fetched through a rest service. Instead you should modify you web.xml to make them available using a `servlet-mapping` for your static content. – atomman Oct 18 '13 at 14:42
  • thanks for the reply! I had a notion that this was not the good way to do it. I am searching on how to servlet map the css, etc. – anothernoc Oct 18 '13 at 14:59
  • 1
    Have a look at: http://stackoverflow.com/questions/12422660/jersey-servlet-mapping-causes-404-error-for-static-resources/12428843#12428843 – condit Oct 18 '13 at 16:04
  • @condit thanks, it looks very helpful. Over the weekend, I thought it over, and I might use XSLT as a way of telling how my data should look like. That might be a better solution. – anothernoc Oct 21 '13 at 10:28

0 Answers0