Reputation: 3723
I am trying to create a REST client for a REST method created using Apache CXF. I used wadl2java command to generate the DTO classes for the client, but they miss the @@XmlRootElement element and so I am trying to use the JAXB object factory, but getting issues.
Please verify my code and the Exception I get.
Source: 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());
}
Client Person.java created using wadl2java command:
@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;
}
}
Jersey Client:
@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();
}
}
Exception received ::
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
Also additionally the below Resource class was created by wadl2java. What is the use of this?
@Path("/")
public interface Resource {
@POST
@Consumes("application/octet-stream")
@Path("sayHello")
void post();
}
Command used to generate the classes from 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.
Below is the WADL generated by CXF:
<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>
Upvotes: 0
Views: 1394
Reputation: 10110
You send a POST that waits for application/octet-stream
in Resource
class, but you send as APPLICATION_XML
in your test case.
Another point is: The post method don't receive any parameter at constructor, so you need to declare the Person
object to be processed to the Resource method.
Just delete the post() method at Resource
class.
[EDIT]
I've found in this call:
service.path("/sampleRestService").path("sayHello").accept(MediaType.APPLICATION_XML).post(person)
the correct is to specify the type of the entity, like:
service.path("/sampleRestService").path("sayHello").type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(Person.class, person)
Upvotes: 1