Dropwizard升级时HttpServletRequest请求导致空指针异常。

huangapple go评论65阅读模式
英文:

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();
    }
}

详见Dropwizard 迁移指南

英文:

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

huangapple
  • 本文由 发表于 2020年9月2日 17:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63702455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定