簡體   English   中英

CXF-JaxRS WebClient的自定義對象輸入

[英]Custom object input for CXF-JaxRS WebClient

我對RESTful很陌生,並嘗試創建一個示例服務來實現void方法上的POST。 我可以測試String類的方法,但是在使用自定義對象進行測試時會得到Exception。

服務類:

@Override
@POST
@Path("/sayHello")
public void sayHello(Person person) {
    System.out.println("Hello there, " + person.getName());         
}

@Override
@POST
@Path("/sayHi")
public void sayHi(String name) {
    System.out.println("Hey there, " + name);       
}   

測試客戶:

public void testSayHelloRest() throws Exception { 
    WebClient client = WebClient.create("http://localhost:8080/ServicesTutorial/sampleService/sayHello");
    Person p = new Person();
    p.setName("My Name");           
    client.post(p);
   }

public void testSayHi() throws Exception {    
    WebClient client = WebClient.create("http://localhost:8080/ServicesTutorial/sampleService/sayHi");  
    client.post("My Name"); 
}

使用簡單的字符串輸入的第二次測試通過,但是第一次測試失敗並顯示以下異常

org.apache.cxf.interceptor.Fault: .No message body writer has been found for class : class com.wk.services.data.Person, ContentType : application/xml.

人類

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }       
}

您需要像這樣注釋您的Person類:

@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Person {
    private String name;

    @XmlElement (name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }       
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM