java springcloud中发布webservice 接口
一、在pom文件中添加依赖:
<!--webservice-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<!-- 这个主要是client访问的,但是问题多多-->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
二、添加配置类
package com.flow.mongodb.wsconfig;
import com.flow.mongodb.service.WbceshijkService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
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 {
@Autowired
private Bus bus;
@Autowired
private WbceshijkService busBuildService;
@Bean
public ServletRegistrationBean disServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/ResWebservice/services/*");
}
@Bean
public Endpoint buildOrderEndpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, busBuildService);
endpoint.publish("/BusBuildService");
return endpoint;
}
}
三、新增接口
package com.flow.mongodb.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "WbceshijkService", targetNamespace = "http://server.webservice.jk.com")
public interface WbceshijkService {
@WebMethod
String jkService(@WebParam(name = "data") String data,@WebParam(name = "data2") String data2);
@WebMethod
String addNe(@WebParam(name="param") String param);
}
四、接口实现类(空间targetNamespace 和接口需保持一致)
package com.flow.mongodb.service.impl;
import com.flow.mongodb.service.WbceshijkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Slf4j
@Component
@WebService( targetNamespace = "http://server.webservice.jk.com",
name = "WbceshijkService")
public class WbceshijkServiceImpl implements WbceshijkService {
@Override
public String jkService(String data, String data2) {
log.info("======emrService===111===="+data);
log.info("======emrService==222====="+data2);
return "操作成功";
}
@Override
public String addNe(String param) {
log.info("======student===111====\n"+param);
return "操作成功";
}
}
五、启动服务,访问接口
由于是在本地启动的,所以启动地址为127.0.0.1,
接口访问地址组成:IP+端口+配置类中写明的路径+?wsdl=
六、通过postman进行对接口调用以及参数说明
1. 访问的接口地址同上(http://127.0.0.1:10069/ResWebservice/services/BusBuildService?wsdl=)
2. 设置请求头参数:Content-Type:text/xml
3. 请求报文
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://server.webservice.jk.com">
<soapenv:Body>
<test:addNe>
<param>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><Data>
<Params>
<name>张三</crm_order_id>
<address>云南昆明西山区</address>
<age>2000</age>
</Params>
</Data>]]>
</param>
</test:addNe>
</soapenv:Body>
</soapenv:Envelope>
4.接口调用返回参数
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:addNeResponse xmlns:ns2="http://server.webservice.jk.com"><return>操作成功</return></ns2:addNeResponse></soap:Body></soap:Envelope>