英文:
Dropwizard upgrade HttpServletRequest request null pointer exception
问题
我已将 Dropwizard 版本从 1.3.12 升级到 2.0.12。
在重新运行我的应用程序后,HttpServletRequest 抛出了空指针异常。
以下是示例代码:
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);
}
}
仅供参考,我已从代码中删除了额外的部分以使其更简单。
有任何建议为何会出现 HttpServletRequest 为空?在 Dropwizard 版本 1.3.12 中它能正常工作。
英文:
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.
答案1
得分: 2
迁移带有字段上下文注入的资源实例到 Dropwizard 2.0 需要将字段推入所需端点的参数中,因此您的类将变成如下形式:
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();
}
}
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论