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();
}


}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。