/
App1.java
49 lines (42 loc) · 1.86 KB
/
App1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.app1;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import com.github.mustachejava.MustacheNotFoundException;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.jersey.optional.EmptyOptionalException;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import io.dropwizard.views.ViewRenderException;
public class App1 extends Application<Configuration> {
@Override
public void initialize(Bootstrap<Configuration> bootstrap) {
bootstrap.addBundle(new ViewBundle<Configuration>());
}
@Override
public void run(Configuration config, Environment env) throws Exception {
// Ensure that we can override the default 404 response on an
// empty optional and return a 204 instead
env.jersey().register(new ExceptionMapper<EmptyOptionalException>() {
@Override
public Response toResponse(EmptyOptionalException exception) {
return Response.noContent().build();
}
});
// Ensure that we can override the 503 response of a view that refers to
// a missing Mustache template and return a 404 instead
env.jersey().register(new ExceptionMapper<WebApplicationException>() {
@Override
public Response toResponse(WebApplicationException exception) {
if (exception.getCause() instanceof ViewRenderException
&& exception.getCause().getCause() instanceof MustacheNotFoundException) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return exception.getResponse();
}
});
env.jersey().register(new App1Resource());
}
}