1.添加依赖:
org.apache.cxf
cxf-spring-boot-starter-jaxws
3.4.3
org.apache.cxf
cxf-rt-transports-http-jetty
3.4.3
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 "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 0 \n" +
" 成功 \n" +
" \n" +
" ";
}
}
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字符串必须用转义):
1.添加依赖:
org.apache.axis
axis
1.4
commons-discovery
commons-discovery
0.2
commons-logging
commons-logging
org.apache.axis
axis-jaxrpc
1.4
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","\n" +
" \n" +
" 30 \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" NFYKDXNFYY \n" +
" \n" +
" E \n" +
" 220708 \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ZZYXKEBQ-重症医学科二病区 \n" +
" \n" +
" NFYKDXNFYY \n" +
" 科室地址 \n" +
" 18665000637#11100637 \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ZZYXKEBQ \n" +
" \n" +
" 2015-05-23 \n" +
" 1 \n" +
" \n" +
" \n" +
" \n" +
" N \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" "};
//实例化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 map = new HashMap<>();
map = XmlUtils.parseXml2Map(result,map);
String responseValue = map.get(responseKey);
System.out.println(responseValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.解析xml字符串工具类:
dom4j
dom4j
1.6.1
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 parseXml2Map(String xml, Map 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;
}
}