Pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.12</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency> </dependencies>
服務接口
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * 網吧web services 接口 * @author xiaojf 2017/7/24 21:35 */ @WebService(targetNamespace = "http://service.netbar.temple.xiaojf.cn")// 命名空間,一般是接口的包名倒序 public interface NetbarServices { @WebMethod String sayHello(@WebParam(name = "userName") String name); }
服務實現
import org.springframework.stereotype.Component; import javax.jws.WebService; /** * 網吧web services 接口實現 * @author xiaojf 2017/7/24 21:38 */ @WebService(serviceName = "NetbarServices"//服務名 ,targetNamespace = "http://service.netbar.temple.xiaojf.cn"//報名倒敘,並且和接口定義保持一致 ,endpointInterface = "cn.xiaojf.temple.netbar.service.NetbarServices")//包名 @Component public class NetbarServicesImpl implements NetbarServices { @Override public String sayHello(String name) { return "hello , "+ name; } }
發布服務
import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; 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 NetbarServices netbarServices; @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus,netbarServices); endpoint.publish("/testservice");//接口發布在 /test 目錄下 return endpoint; } }
驗證
http://localhost:8002/services/mytestservice?wsdl
創建成功以后webservice是在 /services/目錄下
如http://localhost:8080/services/mdmws?wsdl