我正在尝试为使用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());
}
使用wadl2java命令创建客户端Person.java:
@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>
答案 0 :(得分:1)
您在application/octet-stream
课程中发送等待Resource
的POST,但您在测试用例中发送APPLICATION_XML
。
另一点是:post方法在构造函数中不接收任何参数,因此您需要将要处理的Person
对象声明为Resource方法。
只需删除Resource
类的post()方法。
<强> [编辑] 强>
我在这个电话中找到了:
service.path("/sampleRestService").path("sayHello").accept(MediaType.APPLICATION_XML).post(person)
正确的是指定实体的类型,例如:
service.path("/sampleRestService").path("sayHello").type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(Person.class, person)