Issue
I have upgraded the dropwizard version from 1.3.12 to 2.0.12. After running my app again I am getting null pointer exception in HttpServletRequest. Here is the example code
import javax.servlet.http.HttpServletRequest;
public class myClass{
@Context
private HttpServletRequest request;
@GET
@Path("/authenticate")
@Produces(MediaType.TEXT_HTML)
public Response getAuthentication(@QueryParam("myParam") String myParam) {
System.out.println(request);
}
}
just so you know, I have removed the extra bits from the code to make it simple. Any suggestions why getting HttpServletRequestas null ? with dropwizard version 1.3.12 it is working fine.
Solution
Migrating resource instances with field context injections to Dropwizard 2.0 involves pushing the field into a parameter in the desired endpoint, so your class would turn out like this:
public class MyClass {
@GET
@Path("/authenticate")
@Produces(MediaType.TEXT_HTML)
public Response getAuthentication(final @Context HttpServletRequest request,
@QueryParam("myParam") String myParam) {
System.out.println(request);
return Response.ok().build();
}
}
See the Dropwizard Migration Guide
Answered By - Jenneth
Answer Checked By - Senaida (JavaFixing Volunteer)