简体   繁体   English

Dropwizard升级HttpServletRequest请求null指针异常

[英]Dropwizard upgrade HttpServletRequest request null pointer exception

I have upgraded the dropwizard version from 1.3.12 to 2.0.12.我已将 dropwizard 版本从 1.3.12 升级到 2.0.12。 After running my app again I am getting null pointer exception in HttpServletRequest.再次运行我的应用程序后,我在 HttpServletRequest 中收到 null 指针异常。 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?为什么要获取 HttpServletRequestas null 有什么建议吗? with dropwizard version 1.3.12 it is working fine.使用 dropwizard 版本 1.3.12 它工作正常。

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:将具有字段上下文注入的资源实例迁移到 Dropwizard 2.0 涉及将字段推送到所需端点的参数中,因此您的 class 将如下所示:

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请参阅Dropwizard 迁移指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM