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


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值