יום שני, 10 ביוני 2013

CXF DataBinding with JAXB

Declare Simple POJO that will be used as parameter and return value .
Note the JAXB annotations that declare the conversion to XML

  1: package com.mycompany.cxfserver;
  2: 
  3: import javax.xml.bind.annotation.XmlAccessType;
  4: import javax.xml.bind.annotation.XmlAccessorType;
  5: import javax.xml.bind.annotation.XmlElement;
  6: import javax.xml.bind.annotation.XmlRootElement;
  7: import javax.xml.bind.annotation.XmlType;
  8: 
  9: @XmlRootElement(name="MyMsg", namespace=" http://localhost/MyMsg")
 10: @XmlAccessorType(XmlAccessType.FIELD)
 11: @XmlType(name="MyMsg", namespace="http://localhost/MyMsg")
 12: public class MyMsg {
 13:     
 14:     @XmlElement(name="Name", namespace="")
 15:     String Name;
 16: 
 17:     public String getName() {
 18:         return Name;
 19:     }
 20: 
 21:     public void setName(String Name) {
 22:         this.Name = Name;
 23:     }
 24:      @XmlElement(name="Age", namespace="")
 25:     int Age;
 26: 
 27:     public int getAge() {
 28:         return Age;
 29:     }
 30: 
 31:     public void setAge(int Age) {
 32:         this.Age = Age;
 33:     }
 34: }

The SEI declaration:


  1: @org.apache.cxf.interceptor.InInterceptors (interceptors = {"com.mycompany.cxfserver.WebServiceCallsInterceptor" })
  2: @WebService
  3: public interface HelloWorld {
  4:     String sayHi(String text);
  5:     MyMsg GetMsg (MyMsg pMsg);
  6: }

The service


  1: @WebService(endpointInterface = "com.mycompany.cxfserver.HelloWorld")
  2: public class HelloWorldImpl implements HelloWorld {
  3: 
  4:     public String sayHi(String text) {
  5:         return "Hello " + text;
  6:     }
  7: 
  8:     public MyMsg GetMsg(MyMsg pMsg) {
  9:         pMsg.Name = "Hello:" + pMsg.Name;
 10:         pMsg.Age +=1;
 11:         return pMsg;
 12:     }
 13: }
 14: 

Build the Service and run the project in order to allow the client to refresh its wsdl
Capture31



Call to refresh the web service client proxy
Capture32


Add calls to teh new method in the client :


  1: HelloWorldImplService theHelloWorldImplService = new HelloWorldImplService();
  2:         
  3: HelloWorld theHelloWorld = theHelloWorldImplService.getHelloWorldImplPort();
  4:            
  5: cxfclient.MyMsg theMyMsg = new MyMsg();
  6:         
  7: theMyMsg.name = "Zvika";
  8:         
  9: theMyMsg.age = 42;
 10:         
 11: theMyMsg = theHelloWorld.getMsg(theMyMsg);
 12:         
 13: System.out.print(theMyMsg.name + " " + theMyMsg.age);

And the result :
Hello:Zvika 43

אין תגובות:

הוסף רשומת תגובה