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","GBK");//设置传递xml编码 marshaller.marshal(req,sw); System.out.println("-----封装的xml-------"+sw.toString()); } catch (JAXBException e) { e.printstacktrace(); } return sw.toString(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。