Frage

I'm building a single page application which does all of it's html request routing on the client side and on the backend it uses dropwizard to provide a bunch of JSON services.

Essentially I'm having trouble getting the jetty in dropwizard to serve index.html for every request except to the following paths:

  /css
  /i18n
  /img
  /js
  /lib
  /services
  /templates

In fact I'm having a lot of trouble finding documentation that tells you how to setup any http routing at all. (I'm not a java guy).

Here's my simple yaml config:`

http:
  port: 8082
  adminPort: 8083
  rootPath: /service/*`

What do I need to add to acheive this.

Thanks

War es hilfreich?

Lösung

I've done this without changing my configuration. In fact, it only took me one line of code, to be put in the initialize method of my Application class:

bootstrap.addBundle(new AssetsBundle("/app", "/", "index.html", "static"));

Which basically says to serve anything under /app inside my JAR file under the URL pattern /, with index.html as the default file. This bundle will be named static, but you could pick whatever name you like.

Note that I'm using version 0.7.0-rc2 of Dropwizard, I'm not sure whether it works for earlier versions as well.

Andere Tipps

@mthmulders answer worked for me when I went to "/" and would work on other url's while the page had not reloaded, but would not work when I refreshed the page at "/foo/bar/bash" or any other url. I fixed this issue by mapping any 404 response to return back the index.html page instead.

Add your static assets to your initialization method of your Application class:

bootstrap.addBundle(new AssetsBundle("/app", "/", "index.html", "static"));

Add the mapping to your new 404 page to your run method of your Application class:

ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
eph.addErrorPage(404, "/error/404");
environment.getApplicationContext().setErrorHandler(eph);

Add your resource that maps to /error/404

@GET
@Path("/error/404")
@Produces(MediaType.TEXT_HTML)
public Response error404() {
        // get html file from resources here...
        return Response.status(Response.Status.OK)
                .entity("<html>foo</html>")
                .build();
}

After doing this I was able to refresh the page at "/foo/bar/bash" and it still returned back the index.html file.

@jjbskir has the right answer. There are some other solutions here and here and here, but they introduce unnecessary dependencies.

Here is a more complete resource for serving the index page:

@Path("/errorpage")
public class ErrorService {

    private static String indexPage;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Response get404() {

        if (indexPage == null) {
            InputStream in = this.getClass().getResourceAsStream("/assets/index.html");
            try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
                indexPage = scanner.useDelimiter("\\A").next();
            }
        }

        return Response.status(Response.Status.OK)
                .entity(indexPage)
                .build();
    }

}

It caches the index page because it should never change at runtime. You might want to omit the caching if you use this service at development time.

Modify the call to the ErrorPageErrorHandler like this:

eph.addErrorPage(404, "/api/errorpage");

Don't forget the /api prefix if that's where your api is, and don't forget to register the resource.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top