0

I am writing a REST service providing 2 things:
1. Data information (which are read from a database)
2. A web-application (i.e. a webpage)

So currently I only have an index page and I return this html file in a REST request. My Service class for this looks like that:

@Path("web")
public class MainPages {
    @GET
    @Produces(MediaType.TEXT_HTML)
    public InputStream getIndex(){
    File index = new File("webapp/pages/index.html");
    try {
        return new FileInputStream(index);
    } catch (FileNotFoundException e) {
        String s = "ERROR";
        return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
    }
}

Now in the HTML file I try to include some javascript file which is placed in the same folder as the HTML file. The header of the HTML file looks like this:

<html ng-app>
<head>
    <meta charset="utf8" />
    <title>Welcome</title>
    <script type="text/javascript" src="angular.js"></script>
</head>

The problem I am facing now is, that the src is not getting the file but instead the REST Service is activated, as you can see in this image: enter image description here

I think I am missing some important information here.
But my question is: How can I tell the page to look in the folder and not to request the file from the REST?
Or do I need to add the corresponding Path to the REST Service and return the javascript file there?

prashkr
  • 398
  • 2
  • 10
M0rgenstern
  • 411
  • 1
  • 6
  • 18

1 Answers1

1

Serving through JAX-RS resource

If you are not using frameworks such as Play or Spring then you have to manually serve static content.

You can create a public folder which contains the static files and serve them through the following resource method. I haven't tested the code but it will be similar to this.

@Path('{filename}')
@GET
public InputStream getIndex(@PathParam("filename") String fileName){
File index = new File("webapp/public/" + fileName);
try {
    return new FileInputStream(index);
} catch (FileNotFoundException e) {
    String s = "ERROR";
    return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
}  

Resources:

Community
  • 1
  • 1
prashkr
  • 398
  • 2
  • 10