Java jersey客户端在ClientResponse.getLastModified()中抛出NPE

Java jersey客户端在ClientResponse.getLastModified()中抛出NPE,java,rest,jersey,rest-client,Java,Rest,Jersey,Rest Client,我使用jersey客户端使用REST服务。 我需要请求的实体和最后修改的头 因此,我做了以下工作: ClientResponse response = webResource.get(ClientResponse.class); Person person = response.getEntity(Person.class); 这很有效。我得到一个响应,可以将实体(即XML)封送到我的POJO中。 当我调试并查看响应的标题时,我看到有一个最后修改的标题集 但是当我试图通过 response.g

我使用jersey客户端使用REST服务。 我需要请求的实体和最后修改的头

因此,我做了以下工作:

ClientResponse response = webResource.get(ClientResponse.class);
Person person = response.getEntity(Person.class);
这很有效。我得到一个响应,可以将实体(即XML)封送到我的POJO中。 当我调试并查看响应的标题时,我看到有一个最后修改的标题集

但是当我试图通过

response.getLastModified();
我在URLConnectionClientHandler的某个地方得到一个NPE

有人知道我做错了什么吗

根据请求编辑:跟踪

java.lang.NullPointerException: null
at com.sun.jersey.api.client.ClientResponse.getLastModified(ClientResponse.java:647) ~[jersey-client-1.12.jar:1.12]
at a.o.u.user.dao.impl.uds.PersonenUdsClient.getPerson(PersonenUdsClient.java:103) ~[um-user-2.5.0-Beta1-SNAPSHOT.jar:na]
at a.o.u.user.dao.impl.UserDaoUdsImpl.mergeWithUdsUser(UserDaoUdsImpl.java:282) ~[um-user-2.5.0-Beta1-SNAPSHOT.jar:na]
at a.o.u.user.dao.impl.UserDaoUdsImpl.getUserWithEmail(UserDaoUdsImpl.java:124) ~[um-user-2.5.0-Beta1-SNAPSHOT.jar:na]
at ...
编辑:根据npe的建议,我深入研究了代码。我想我发现了问题所在。除了jersey客户端之外,类路径中还有cxf。jersey和cxf都提供了一个名为RuntimeDelegateImpl的类。但CXFs版本没有DateHeaderDelegate。我认为采用了错误的RuntimeDelegateImpl版本(CXFs)

到目前为止,我还没有找到如何明确地设置要使用的RuntimeDelegateImpl。

版本1.12的
ClientResponse#getLastModified()
的实现如下所示:

/*639*/  /**
/*640*/   * Get the last modified date.
/*641*/   *
/*642*/   * @return the last modified date, otherwise <code>null</code> if not present.
/*643*/   */
/*644*/  public Date getLastModified() {
/*645*/      String d = getHeaders().getFirst("Last-Modified");
/*646*/  
/*647*/      return (d != null) ? dateDelegate.fromString(d) : null;
/*648*/  }
/*321*/  protected static final HeaderDelegate<Date> dateDelegate =
/*322*/          RuntimeDelegate.getInstance().createHeaderDelegate(Date.class);
/* 88*/  map.put(Date.class, _createHeaderDelegate(Date.class));
您在第647行中得到了一个
NullPointerException
,因此显示
dateDelegate
null
。现在,
dateDelegate
对象在第321行初始化,如下所示:

/*639*/  /**
/*640*/   * Get the last modified date.
/*641*/   *
/*642*/   * @return the last modified date, otherwise <code>null</code> if not present.
/*643*/   */
/*644*/  public Date getLastModified() {
/*645*/      String d = getHeaders().getFirst("Last-Modified");
/*646*/  
/*647*/      return (d != null) ? dateDelegate.fromString(d) : null;
/*648*/  }
/*321*/  protected static final HeaderDelegate<Date> dateDelegate =
/*322*/          RuntimeDelegate.getInstance().createHeaderDelegate(Date.class);
/* 88*/  map.put(Date.class, _createHeaderDelegate(Date.class));
这个兔子洞越来越深,所以我就到此为止,但你知道路

最后,但并非最不重要——调试器是你的朋友,我的朋友;-)

将其包装起来

问题是类路径中既有jersey客户端也有cxf。两个运行时delegateimpl。但CXFs版本没有DateHeaderDelegate。采用了错误的RuntimeDelegateImpl版本(CXFs)

我通过“手动”检索上次修改的标题解决了此问题:

private Date getLastModified(ClientResponse response){
    String lastModifiedString = response.getHeaders().getFirst(
            "Last-Modified");
    if (StringUtils.isEmpty(lastModifiedString)) {
        LOG.warn("Expect to get Last-Modified header when retrieving a Person by pnr "
                + "but there is none.");
        return null;
    } else {
        try {
            // format is Thu, 21 Jun 2012 08:00:42 GMT
            return new SimpleDateFormat(
                    "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US)
                    .parse(lastModifiedString);
        } catch (ParseException e) {
            LOG.error("Could not parse Last-Modified date "
                    + e.getMessage());
            return null;
        }
    }
}

Thanx向npe发送提示。

正确,源代码总是有用的。但我认为像dateDelate这样的东西应该就在那里,而不是我做什么特别的事。对我来说,我看起来很基本。我的初始化代码不正确吗?我使用client.create()初始化我的客户机;然后我得到我的WebResource,比如getClient().resource(baseUrl);我也可以使用ClientConfig来创建客户端,但是里面没有任何东西可以看出SM与dateDelegate有关,您会发现在
jersey core.jar
META-INF/services
文件夹中有一个
com.sun.jersey.spi.HeaderDelegateProvider
文件,用于定义代理。除非您正在使用这些JAR(比如删除
META-INF
),否则我真的不知道为什么它没有加载。我认为您需要调试Jersey的初始化代码,我已经向您指出,以找出答案。