2013-11-26 68 views
0

從Resteasy 2.4升級到3.0.5後,我的客戶端調用再也無法工作。我期待的實體(就像String一樣簡單)始終爲空。在調試模式下,我可以看到它在服務器端的Response對象中設置,但在客戶端,Response中的實體爲空。下面是一個例子電話:用RestEasyWebTarget替換ProxyFactory不起作用

接口:

/** 
    * @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

回答

0

所以,我已經更新我的架構與JAXB解決我的問題:版本= 「2.0」 和重新生成jaxb類。該提示有人提到存在Resteasy Client - > JAX-RS 2.0不匹配遷移問題。 另外要注意的是,在這個jaxb版本中,XaRootElementis仍然被生成器省略,所以它需要被添加到所有用作POST請求輸入的類中