webservices在前幾年是非常流行的,后來rest-api出世后簡直是顛覆了ws的天下.
但是在項目中還是有小部分會使用到ws,這里就簡單介紹下spring-boot添加ws支持
官方的支持也是只給了簡短的說明
在這里只是簡單介紹如何快速搭建一個ws項目, 案例代碼在 spring-boot(九)websocket配置 基礎之上改造
項目結構圖
在pom中添加ws的依賴, 本案例通過cxf來搭建ws
<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.1.7</version>
</dependency>
在包 hello.jax.ws
下
新建一個接口 Hello.java
package hello.jax.ws;
/**
* @author majinding888@foxmail.com
* @date 2016-12-30 下午5:28:59
*/
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "Hello")
public interface Hello {
@WebResult()
@WebMethod()
String sayHello(@WebParam(name = "myname") String myname);
}
新建一個實現(xiàn)類 HelloPortImpl.java
package hello.jax.ws;
import javax.jws.WebService;
/**
* @author majinding888@foxmail.com
* @date 2016-12-30 下午5:29:29
*/
@WebService
public class HelloPortImpl implements Hello {
public String sayHello(String myname) {
return "Hello, Welcome to CXF Spring boot " + myname + "!!!";
}
}
新建 WebServiceConfig.java
配置,啟用ws
package hello.jax.ws;
/**
* @author majinding888@foxmail.com
* @date 2016-12-30 下午5:30:09
*/
import javax.xml.ws.Endpoint;
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;
@Configuration
public class WebServiceConfig {
@Autowired
private Bus bus;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
endpoint.publish("/Hello");
return endpoint;
}
}
到此ws環(huán)境就搭建完成,啟動瀏覽器 訪問 http://localhost:666/services/Hello?wsdl
(切記需要在發(fā)布路徑前面增加services這個path)
源代碼附件地址: my-springboot-10.zip