Java 用RestEasyWebTarget替换ProxyFactory无效
从Resteasy 2.4升级到3.0.5后,我的客户机再也不能工作了。我期望的实体(像字符串一样简单)总是空的。在调试模式下,我可以看到它被设置在服务器端的响应对象中,但在客户端,响应中的实体为null。下面是一个示例调用: 界面:Java 用RestEasyWebTarget替换ProxyFactory无效,java,jaxb,client,jax-rs,resteasy,Java,Jaxb,Client,Jax Rs,Resteasy,从Resteasy 2.4升级到3.0.5后,我的客户机再也不能工作了。我期望的实体(像字符串一样简单)总是空的。在调试模式下,我可以看到它被设置在服务器端的响应对象中,但在客户端,响应中的实体为null。下面是一个示例调用: 界面: /** * @return all expired licenses */ @GET @ClientResponseType(entityType = LicenseList.class) @Path("/expired") @Pr
/**
* @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" })));
}
}
*
*
*
*列表中允许以下类型的对象
*{@link License}
*
*
*/
公共列表getLicenses(){
如果(许可证==null){
许可证=新的ArrayList();
}
退还此许可证;
}
}
我使用的是JDK1.7、Tomcat7.0.47,因此,我通过使用jaxb:version=“2.0”更新模式解决了我的问题 以及重新生成jaxb类。消息是有人提到有一个 Resteasy客户端->JAX-RS 2.0不匹配迁移问题。 还要注意的是,在这个jaxb版本中,Generator仍然省略了XMLRootElement,因此需要将它添加到所有用作POST请求输入的类中
<?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>
@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;
}
}