概述
詳細
一、創建springboot項目
1.新建一個springboot項目,不需要添加任何依賴。

2.在啟動類中編寫一個接口,然后啟動項目,訪問http://localhost:8080測試項目是否存在問題。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/")
public String hello(){
return "hello spring boot";
}
}
二、編寫webservice接口
-
在com.example.demo路徑下新建webservice目錄,並在該目錄下新建webservice接口TestService.java,編寫一個webService接口方法。
-
在該接口上使用javax.jws.WebServcie注解;在方法上使用javax.jws.WebMethod注解。
package com.example.demo.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface TestService {
@WebMethod
String hiWebService(@WebParam(name = "hi") String s);
}
3.在相同路徑下新建TestService接口的實現類TestServiceImpl.java,實現接口方法。並且在該類上使用javax.jws.WebService注解和org.springframework.stereotype.Service注解,是該類即是一個webService接口服務類又是作為一個可以被spring管理的bean。
package com.example.demo.webservice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@WebService
@Service
public class TestServiceImpl implements TestService{
private Log log = LogFactory.getLog(TestServiceImpl.class);
@Override
public String hiWebService(String s) {
String msg = "獲取內容:"+s;
log.info(msg);
return msg;
}
}
三、啟動配置
-
使用ApplicationContext事件機制來完成webService接口的自動發布。
使用ApplicationListener監聽ContextRefreshedEvent事件。ContextRefreshedEvent就是在ApplicationContext被初始化(所有的bean被成功裝載,后處理bean被檢測或成功激活,所有的singleton bean被實例化,ApplicationConte容器已就緒可用)或刷新時,該事件被發布。
-
在webservice目錄下新建一個類BeforeStartUp.java,實現ApplicationListene接口。
-
重寫onApplicationEvent方法。在該方法中發布webService服務。使用javax.xml.ws.Endpoint的publish方法發布。該方法有兩個參數,第一個是要使用的地址和傳輸/協議的URI。URI必須使用SOAP 1.1 / HTTP 綁定。第二個參數是webService的接口實現類。
-
該類也必須使用@Service,@Component或者@Configuration注解被spring管理,使其可以被自動裝載。
package com.example.demo.webservice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import javax.xml.ws.Endpoint;
@Service
public class BeforeStartUp implements ApplicationListener<ContextRefreshedEvent>{
private Log log = LogFactory.getLog(BeforeStartUp.class);
// @Value("${spring.ws.address}")
private static String address = "http://localhost:8002/ws/hello";
@Autowired
private TestService testService;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Endpoint.publish(address,testService);
log.info("webService 服務發布成功!!!");
log.info("wsdl地址:"+address+"?wsdl");
}
}
四、啟動應用,自動發布
-
啟動應用程序,觀察控制台日志。出現如下日志即表示服務發布成功。

點擊wsdl地址即可在瀏覽器上查看該webService的wsdl的內容。
由於webService是跨平台的。只要通過該wsdl文件就可以生成相應平台上的客戶端或者服務端代碼。
五、生成客戶端代碼(使用的是IDEA編輯器),通過客戶端訪問webservice接口
-
新建一個Java項目。

-
右擊client,選擇webService,選擇generate java code from wsdl

3.

4.點擊ok即可生成客戶端代碼。調用接口跟平常寫程序調用方法沒什么兩樣。方法在TestServiceImpl中,獲取TestServiceImpl對象的方法在TestServcieImplService中。在main方法中調用:
package main.java;
import main.client.TestServiceImpl;
import main.client.TestServiceImplService;
public class Test {
public static void main(String[] args){
TestServiceImpl service = (new TestServiceImplService()).getTestServiceImplPort();
String s = service.hiWebService("hi webService!");
System.out.println(s);
}
}
六、運行main方法、查看接口調用
客戶端:

說明接口已經成功調用。
服務端:

七、項目結構
本例子分為兩部分,有客戶端,也有webservice端,如下所示:

