记一次SpringBoot集成WebService使用Postman进行的测试
首先需要引入包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency>
<!--读写XML--> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
<!--在jdom中使用xpath需要导入jaxen.jar包--> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
然后就需要写一个接口
package com.qiao.aopdemo.webservice; import org.dom4j.DocumentException; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import java.io.UnsupportedEncodingException; @WebService(name = "ITSMService", //暴露服务名称 targetNamespace = "http://webService.service.interact.com/" //目标命名空间 一般是接口包名的倒叙 ) public interface ITSMService { @WebMethod public String createWorkOrder(@WebParam(name = "xmlStr") String xmlStr) throws UnsupportedEncodingException, DocumentException; }
接口实现类
package com.qiao.aopdemo.webservice; import com.qiao.aopdemo.util.WebServiceUtil; import org.dom4j.Document; import org.dom4j.DocumentException; import javax.jws.WebParam; import javax.jws.WebService; import java.io.UnsupportedEncodingException; @WebService( endpointInterface = "com.qiao.aopdemo.webservice.ITSMService", targetNamespace = "http://webService.service.interact.com/" ) public class ITSMServiceImpl implements ITSMService { @Override public String createWorkOrder(@WebParam(name = "xmlStr") String xmlStr) throws UnsupportedEncodingException, DocumentException {
return xmlStr;
} }
配置类
package com.qiao.aopdemo.webservice; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration public class CxfConfig { @Bean public ServletRegistrationBean dispatcherCXFServlet() { return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), new ITSMServiceImpl()); endpoint.publish("/itsmService"); return endpoint; } }
工具类
package com.qiao.aopdemo.util; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.*; public class WebServiceUtil { public static Document getDocument(String returnXmlData) throws UnsupportedEncodingException, DocumentException { SAXReader reader = new SAXReader(); return reader.read(new ByteArrayInputStream(returnXmlData.getBytes("UTF-8"))); } public static String getSingleElementText(Document documen,String xpath) { Element ele=(Element)documen.selectSingleNode(xpath); if(ele==null){ return null; }else { return ele.getText(); } } }
启动SpringBoot项目,访问 http://localhost:8080/webService/itsmService?wsdl 会得到以下结果,内容有点多就不截全了,仔细看看,基本上方法,接口名都在以下内容中了
接下来就可以进行测试了,我使用的是Postman
第一步:
第二步:
具体代码:
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://webService.service.interact.com/"> <soapenv:Body> <test:createWorkOrder> <xmlStr><![CDATA[测试]]> </xmlStr> </test:createWorkOrder> </soapenv:Body> </soapenv:Envelope>
返回结果:图片中有个标红的写错了,不是targeSpaceName 而是tagetNameSpace
还在继续研究,先暂时做个记录。
使用客户端测试代码,如下:测试可以使用Test,也可以直接使用main方法,参考链接:https://blog.csdn.net/sujin_/article/details/83865124
@Test public void testSend1(){ // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); //Client client = dcf.createClient("http://localhost:8080/webService/itsmService?wsdl"); Client client = dcf.createClient("http://localhost:8080/webservice/itsmService?wsdl"); // 需要密码的情况需要加上用户名和密码 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD)); Object[] objects = new Object[0]; try { String param = "你想要传递的内容,也可以是xml"; // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("createWorkOrder", param); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } }