码迷,mamicode.com
首页 > Web开发 > 详细

WebService之axis的应用

时间:2015-01-20 12:07:53      阅读:140      评论:0      收藏:0      [点我收藏+]

标签: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的应用

标签:webservice   axis   

原文地址:http://blog.csdn.net/hlf1203/article/details/42914535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!