我正在尝试为使用 Apache CXF 创建的 REST 方法创建一个 REST 客户端。我使用 wadl2java 命令为客户端生成 DTO 类,但它们错过了 @@XmlRootElement 元素,因此我尝试使用 JAXB 对象工厂,但遇到了问题。
请验证我的代码和我得到的异常。
来源:Person.java
@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Person {
private String name;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
SampleRestService.java
@POST
@Path("/sayHello")
public void sayHello(Person person) {
System.out.println("Hello there!!!");
System.out.println("Your name -> " + person.getName());
}
客户端 Person.java 使用 wadl2java 命令创建:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "person", propOrder = {
"name"
})
public class Person {
protected Address address;
protected String name;
/**
* Gets the value of the address property.
*
* @return
* possible object is
* {@link Address }
*
*/
public Address getAddress() {
return address;
}
/**
* Sets the value of the address property.
*
* @param value
* allowed object is
* {@link Address }
*
*/
public void setAddress(Address value) {
this.address = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
泽西岛客户:
@Test
public void testSayHelloObjectFactory(){
try {
ObjectFactory objectFactory=new ObjectFactory();
com.wk.client.data.Person person=objectFactory.createPerson();
person.setName("Saravanan");
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
service.path("/sampleRestService").path("sayHello").accept(MediaType.APPLICATION_XML).post(person);
} catch (Exception e) {
e.printStackTrace();
}
}
收到异常 ::
com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.wk.client.data.Person, and MIME media type, application/octet-stream, was not found
此外,下面的资源类也是由 wadl2java 创建的。这个有什么用?
@Path("/")
public interface Resource {
@POST
@Consumes("application/octet-stream")
@Path("sayHello")
void post();
}
用于从 WADL 生成类的命令:
C:\apache-cxf-2.4.2\bin>wadl2java -p com.wk.rest.client http://localhost:8080/Re
stService/sampleRestService?_wadl&_type=xml
May 29, 2013 7:44:02 PM org.apache.cxf.jaxb.JAXBUtils logGeneratedClassNames
INFO: Created classes: generated.Address, generated.ObjectFactory, generated.Per
son
'_type' is not recognized as an internal or external command,
operable program or batch file.
下面是 CXF 生成的 WADL:
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema"><grammars><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified">
<xs:element name="Address" type="address"/>
<xs:element name="Person" type="person"/>
<xs:complexType name="person">
<xs:sequence>
<xs:element minOccurs="0" name="address" type="address"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="address">
<xs:sequence>
<xs:element minOccurs="0" name="city" type="xs:string"/>
<xs:element minOccurs="0" name="streetName" type="xs:string"/>
<xs:element name="streetNumber" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars><resources base="http://localhost:8080/RestService/sampleRestService"><resource path="/"><resource path="sayHello"><method name="POST"><request><representation mediaType="application/octet-stream"/></request><response status="204"></response></method></resource></resource></resources></application>