大家好,我是烤鸭:
这篇文章分享的是xcf webservice接口调用。如果接口提供方要求是webservice调用的话,可以参考这篇内容。
1. pom文件
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.2.5</version>
</dependency>
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.2.5</version>
</dependency>
2. xml报文构造
首先看一下,接口方要求的xml格式。
<?xml version="1.0" encoding="GBK" standalone="yes"?>
<ROOT><request_info><PHONE_NO>234</PHONE_NO><ORDER_ID>123</ORDER_ID></request_info>
</ROOT>
首先构造实体类
RequestRootWebservice.java (xml的ROOT节点)
package com.test.test.webservice;import lombok.Data;
import javax.xml.bind.annotation.*;@XmlAccessorType(value=XmlAccessType.FIELD)
@XmlRootElement(name = "ROOT")
@Data
public class RequestRootWebservice {private RequestInfo request_info;
}
RequestInfo.java (xml子节点)
package com.test.test.webservice;import lombok.Data;import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;/*** Created by gmwang on 2018/7/24*/
@Data
@XmlRootElement(name = "request_info")
@XmlType(propOrder = { "PHONE_NO", "ORDER_ID"})
public class RequestInfo {private String PHONE_NO;private String ORDER_ID;
}
序列化和反序列化的结果:
3. 请求代码
public void testSend1() {// 创建动态客户端JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://localhost:8080/services/TestService?wsdl");// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor("admin","123345"));QName name=new QName("http://service.policyconfirm.apps.gateway.sinosoft.com/","getStudents");try {RequestRootWebservice root = new RequestRootWebservice();RequestInfo requestInfo = new RequestInfo();requestInfo.setORDER_ID("123");requestInfo.setPHONE_NO("234");root.setRequest_info(requestInfo);JAXBContext context = JAXBContext.newInstance(RequestRootWebservice.class);Marshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");marshaller.marshal(root, System.out);StringWriter writer = new StringWriter();marshaller.marshal(root, writer);
// System.out.println(writer.toString());//反序列化Unmarshaller unmarshaller = context.createUnmarshaller();StringReader reader = new StringReader(writer.toString());RequestRootWebservice stu2 = (RequestRootWebservice) unmarshaller.unmarshal(reader);System.out.println("stu2"+stu2);Object[] objects = client.invoke(name,writer.toString());String res = "";if(objects != null && objects.length != 0){res = objects[0].toString();}System.out.println("返回数据:" + res);} catch (Exception e) {e.getMessage();e.printStackTrace();}}