且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式正确配置Jersey资源

更新时间:2023-01-12 12:33:08

您的lambda有问题。 基本上,您有

Inflector<ContainerRequestContext, ?> lambda = (ContainerRequestContext ctx) -> {
                return Response.ok("Posted Profile {" + (ctx == null) + "}").build();
};

如果检查方法参数类型:

for (Method m : lambda.getClass().getMethods()) {
  if ("apply".equals(m.getName())) {
     for (Type t : m.getGenericParameterTypes()) {
         System.out.println(t); //java.lang.Object
     }
  }
}

您得到java.lang.Object。因此,Jersey尝试从实体流获取Object类型,并获取null

所以您需要告诉java通过一个真正的匿名类使用ContainerRequestContext参数类型:

Inflector<ContainerRequestContext, Response> anonymous = new Inflector<>() {
    @Override
    public Response apply(ContainerRequestContext ctx) {
        return Response.ok("Posted Profile {" + (ctx == null) + "}").build();
    }
};

for (Method m : anonymous.getClass().getMethods()) {
    if ("apply".equals(m.getName())) {
        for (Type t : m.getGenericParameterTypes()) {
            System.out.println(t); // ContainerRequestContext 
        }
    }
}