用RestEasyWebTarget替换ProxyFactory不起作用

时间:2013-11-26 00:38:56

标签: java jaxb client jax-rs resteasy

从Resteasy 2.4升级到3.0.5后,我的客户呼叫都不再有效。我期待的实体(像String一样简单)始终为null。在调试模式下,我可以看到它在服务器端的Response对象中设置,但在客户端,Response中的实体为null。这是一个示例调用:

界面:

  /**
   * @return all expired licenses
   */
  @GET
  @ClientResponseType(entityType = LicenseList.class)
  @Path("/expired")
  @Produces(MediaType.APPLICATION_XML)
  public Response getExpiredLicenses();

实施:

  /**
   * @return all expired licenses
   */
  @Override
  public Response getExpiredLicenses() {
    try {
      LicenseList list = LicensesDBUtil.getExpiredLicenses();
      Response resp = Response.ok().entity(list).build();
      return resp;
    } catch (SQLException e) {
      LOG.error("Error getting expired licenses : " + e.getMessage());
      return   Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
    }

测试电话:

  @Test
  public void testGetExpiredLicenses() throws Exception {
        ResteasyClientBuilder rsb = new ResteasyClientBuilder();
    ResteasyClient rsc = rsb.build();
    ResteasyWebTarget target = rsc.target(BASEURL);
    return target.proxy(RiskScapeLicenseService.class);
    Response response = client.getExpiredLicenses();
    assertTrue(HttpResponseCodes.SC_OK == response.getStatus());
    @SuppressWarnings("unchecked")
    JAXBElement<LicenseList> element = (JAXBElement<LicenseList>) response.getEntity();
    LicenseList list = element.getValue();
    assertEquals(4, list.getLicenses().size());
    for (License lic : list.getLicenses()) {
      assertTrue((new Date()).after(DateUtils.parseDate(lic.getValidTo(), new String[] { "yyyy-MM-dd" })));
    }
  }

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>riskscapelic_rest</display-name>
  <listener>
    <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
  </listener>
  <servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/</param-value>
  </context-param>
</web-app>

LicenseList类:

@XmlRootElement(name = "LicenseList")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "licenseList", propOrder = {
    "licenses"
})
public class LicenseList {

    @XmlElement(name = "Licenses", required = true)
    protected List<License> licenses;

    /**
     * Gets the value of the licenses property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the licenses property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getLicenses().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link License }
     * 
     * 
     */
    public List<License> getLicenses() {
        if (licenses == null) {
            licenses = new ArrayList<License>();
        }
        return this.licenses;
    }

}

我正在使用JDK 1.7,Tomcat 7.0.47

1 个答案:

答案 0 :(得分:0)

所以,我通过使用jaxb更新我的架构解决了我的问题:version =“2.0” 并重新生成jaxb类。提示是有人提到有一个 Resteasy客户端 - &gt; JAX-RS 2.0不匹配迁移问题。 另请注意,在此jaxb版本中,XmlRootElement仍然被发生器省略,因此需要将其添加到所有用作POST请求输入的类中