一、服务端:
1.添加依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.4.3</version>
</dependency>
2.创建webService接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
//name:与接口中的name一致,区分作用,无太大意义
//targetNamespace:与接口中的命名空间一致,一般是接口的包名倒写
@WebService(name = "WebServiceDemo", targetNamespace = "http://service.webServicetest.docus.cn")
public interface WebServiceDemo {
@WebMethod
String webServiceTest(@WebParam String action,@WebParam String xml);
}
3.创建实现类:
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class WebServiceDemoImpl implements WebServiceDemo {
@Override
public String webServiceTest(String action,String xml) {
log.info("xml:{}",xml);
return "<Response>\n" +
" <Header>\n" +
" <SourceSystem></SourceSystem>\n" +
" <MessageID></MessageID>\n" +
" </Header>\n" +
" <Body>\n" +
" <ResultCode>0</ResultCode>\n" +
" <ResultContent>成功</ResultContent>\n" +
" </Body>\n" +
"</Response>";
}
}
4.配置webService发布服务
import cn.docus.webservicetest.service.WebServiceDemo;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Slf4j
@Configuration
public class WebServiceConfig {
@Autowired
private WebServiceDemo webServiceDemo;
@Autowired
private Bus bus;
@Value("${server.port}")
private String port;
private static final String path = "/api";
@Bean
public Endpoint userServiceEndpoint() {
EndpointImpl userEndpoint = new EndpointImpl(bus, webServiceDemo);
userEndpoint.publish(path);
log.info("在线的wsdl:http://localhost:"+port+"/services{}?wsdl",path);
return userEndpoint;
}
}
二、测试
浏览器输入:http://localhost:8080/services/api?wsdl
或者使用soapUI工具测试(xml字符串必须用<![CDATA[ ]]>转义):?
三、客户端:
1.添加依赖:
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
2.执行调用:
import cn.docus.webservicetest.utils.XmlUtils;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class WebservicetestApplicationTests {
@Test
void contextLoads() {
try {
String url = "http://localhost:8080/services/api?wsdl";
String namespaceURI = "http://service.webServicetest.docus.cn";
String webServiceMethod = "webServiceTest";
String paramName1 = "arg0";
String paramName2 = "arg1";
String[] params = {"123456","<Request>\n" +
" <Header>\n" +
" <SourceSystem>30</SourceSystem>\n" +
" <MessageID></MessageID>\n" +
" </Header>\n" +
" <Body>\n" +
" <CT_DeptList>\n" +
" <CT_Dept>\n" +
" <BusinessFieldCode>NFYKDXNFYY</BusinessFieldCode>\n" +
" <CTD_AdmType></CTD_AdmType>\n" +
" <CTD_CategoryCode>E</CTD_CategoryCode>\n" +
" <CTD_Code>220708</CTD_Code>\n" +
" <CTD_CodesystemCode></CTD_CodesystemCode>\n" +
" <CTD_CreatDate></CTD_CreatDate>\n" +
" <CTD_CreatTime></CTD_CreatTime>\n" +
" <CTD_DepartmentGroup></CTD_DepartmentGroup>\n" +
" <CTD_Desc>ZZYXKEBQ-重症医学科二病区</CTD_Desc>\n" +
" <CTD_EndDate></CTD_EndDate>\n" +
" <CTD_HosCode>NFYKDXNFYY</CTD_HosCode>\n" +
" <CTD_OfficeAddress>科室地址</CTD_OfficeAddress>\n" +
" <CTD_OfficePhone>18665000637#11100637</CTD_OfficePhone>\n" +
" <CTD_OriginalCode></CTD_OriginalCode>\n" +
" <CTD_OriginalDesc></CTD_OriginalDesc>\n" +
" <CTD_ParentCode></CTD_ParentCode>\n" +
" <CTD_ParentDeptCode></CTD_ParentDeptCode>\n" +
" <CTD_Phonetic>ZZYXKEBQ</CTD_Phonetic>\n" +
" <CTD_Remarks></CTD_Remarks>\n" +
" <CTD_StartDate>2015-05-23</CTD_StartDate>\n" +
" <CTD_Status>1</CTD_Status>\n" +
" <CTD_UpdateDate></CTD_UpdateDate>\n" +
" <CTD_UpdateTime></CTD_UpdateTime>\n" +
" <CTD_UpdateUserCode></CTD_UpdateUserCode>\n" +
" <CTD_VirtualFlag>N</CTD_VirtualFlag>\n" +
" <CTD_WardFlag></CTD_WardFlag>\n" +
" </CT_Dept>\n" +
" </CT_DeptList>\n" +
" </Body>\n" +
"</Request>"};
//实例化org.apache.axis.client.Service对象
Service service = new Service();
//创建service调用对象
Call call = (Call) service.createCall();
//设置发布地址
call.setTargetEndpointAddress(url);
//设置命名空间与方法名
call.setOperationName(new QName(namespaceURI,webServiceMethod));
//按顺序设置请求参数、请求类型,多个需多添加参数
//设置第一个参数
call.addParameter(paramName1, XMLType.XSD_STRING, ParameterMode.IN);
//设置第二个参数
call.addParameter(paramName2, XMLType.XSD_STRING, ParameterMode.IN);
//设置返回类型
call.setReturnType(XMLType.XSD_STRING);
//执行调用
String result = (String) call.invoke(params);
System.out.println(result);
//解析xml
String responseKey = "Body.ResultCode";
Map<String,String> map = new HashMap<>();
map = XmlUtils.parseXml2Map(result,map);
String responseValue = map.get(responseKey);
System.out.println(responseValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
?3.解析xml字符串工具类:
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Map;
public class XmlUtils {
/**
* xml转map
* @param xml
* @param map
* @return
*/
public static Map<String, String> parseXml2Map(String xml, Map<String, String> map) {
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(new StringReader(xml));
Element root = doc.getRootElement();
String path = "";
if (map.containsKey(root.getName().trim())) {
path = map.get(root.getName().trim());
map.remove(root.getName().trim());
}
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if (element.isTextOnly()) {
if (path.length() > 0) {
map.put(path + element.getName().trim(), element.getTextTrim());
} else {
map.put(element.getName().trim(), element.getTextTrim());
}
} else {
map.put(element.getName().trim(), path+ element.getName().trim() + ".");
parseXml2Map(element.asXML(), map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
|