WebService之axis的應用

來源:互聯網
上載者:User

標籤:webservice   axis   

import java.io.StringReader;
import java.io.StringWriter;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;
import com.name.pojo.son.Res;
import com.name.pojo.Req;


public class TestSxWs {


@SuppressWarnings("static-access")
//bean to xml
@Test
public void testWsRes(){
StringWriter sw = new StringWriter();
  Req req = new Req("code","111111111111","","類1","192.168.1.148","2014-08-11");
        try {
           JAXBContext context = JAXBContext.newInstance(Req.class); 
           Marshaller marshaller = context.createMarshaller();
           marshaller.setProperty(marshaller.JAXB_ENCODING, "GBK");
           marshaller.marshal(req, sw);
           System.out.println("-----封裝的xml-------"+sw.toString());
         } catch (JAXBException e) {  
             e.printStackTrace();  
         }
    } 


//調WebService
@Test
public void testSxWs(){
try {
// RPCServiceClient是RPC方式調用
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
// 設定調用WebService的URL
String address = "http://192.168.1.16/services/HndyService";
EndpointReference epf = new EndpointReference(address);
options.setTo(epf);

/**
* 
* QName命名空間參數就是http://axis2.com, HelloWorldService服務類包名倒過來即可
* hello就是方法名稱了
*/
QName qname = new QName("http://service.yz.zyz.modules.whcss.com", "InsertSxInfo");
// 指定調用的方法和傳遞參數資料,及設定返回值的類型
Object[] result = client.invokeBlocking(qname, new Object[] {reqXml1()},
new Class[] { String[].class });
String[] obj = (String[]) result[0];

System.out.println("---傳送過來的xml----"+obj[0]);

try {
            JAXBContext context = JAXBContext.newInstance(Res.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Res u = (Res)unmarshaller.unmarshal(new StringReader(obj[0]));
            //返回的東西
            System.out.println("狀態:"+u.getData().getZt()+","+u.getResult().getMessage()+","+u.getResult().getCode()); 
        } catch (JAXBException e) {
            e.printStackTrace();  
        }

   } catch (AxisFault axisFault) {  
     axisFault.printStackTrace();  
   }
   
}

//封xml
@SuppressWarnings("static-access")
private static String reqXml1() {
StringWriter sw = new StringWriter();
  Req req = new Req("code","2222222222","1","類別1","192.168.1.18","2014-08-11");
        try {
           JAXBContext context = JAXBContext.newInstance(Req.class); 
           Marshaller marshaller = context.createMarshaller();
           marshaller.setProperty(marshaller.JAXB_ENCODING, "GBK");//設定傳遞xml編碼
           marshaller.marshal(req, sw);
           System.out.println("-----封裝的xml-------"+sw.toString());
         } catch (JAXBException e) {  
             e.printStackTrace();  
         }
         return sw.toString();
}


}

WebService之axis的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.