简体   繁体   English

CXF Rest Service的Jersey客户问题

[英]Jersey Client issue for CXF Rest Service

I am trying to create a REST client for a REST method created using Apache CXF. 我正在尝试为使用Apache CXF创建的REST方法创建REST客户端。 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. 我使用wadl2java命令为客户端生成DTO类,但是它们错过了@@ XmlRootElement元素,因此我尝试使用JAXB对象工厂,但遇到了问题。

Please verify my code and the Exception I get. 请验证我的代码和我得到的异常。

Source: Person.java 资料来源: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 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: 使用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;
    }

}

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. 另外,下面的Resource类是由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: 用于从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: 以下是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>

You send a POST that waits for application/octet-stream in Resource class, but you send as APPLICATION_XML in your test case. 您在Resource类中发送了一个等待application/octet-stream的POST,但在测试用例中以APPLICATION_XML发送。

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. 另一点是:post方法在构造函数中未接收任何参数,因此您需要向Resource方法声明要处理的Person对象。

Just delete the post() method at Resource class. 只需删除Resource类中的post()方法。

[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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM