1.什么是webservice
簡單來說,webservice就是遠(yuǎn)程調(diào)用技術(shù),也叫XML Web Service WebService是一種可以接收從Internet或者Intranet上的其它系統(tǒng)中傳遞過來的請求撑螺,輕量級的獨(dú)立的通訊技術(shù)芒帕。是:通過SOAP在Web上提供的軟件服務(wù),使用WSDL文件進(jìn)行說明,并通過UDDI進(jìn)行注冊钉鸯。
XML:(Extensible Markup Language)擴(kuò)展型可標(biāo)記語言季惯。面向短期的臨時數(shù)據(jù)處理咬最、面向萬維網(wǎng)絡(luò)翎嫡,是Soap的基礎(chǔ)。
Soap:(Simple Object Access Protocol)簡單對象存取協(xié)議永乌。是XML Web Service 的通信協(xié)議惑申。當(dāng)用戶通過UDDI找到你的WSDL描述文檔后具伍,他通過可以SOAP調(diào)用你建立的Web服務(wù)中的一個或多個操作。SOAP是XML文檔形式的調(diào)用方法的規(guī)范圈驼,它可以支持不同的底層接口人芽,像HTTP(S)或者SMTP。
WSDL:(Web Services Description Language) WSDL 文件是一個 XML 文檔碗脊,用于說明一組 SOAP 消息以及如何交換這些消息啼肩。大多數(shù)情況下由軟件自動生成和使用橄妆。
2.springboot2.0集成webservice
由于springboot提供了webservice的starter組件衙伶,所以集成webservice相當(dāng)簡單
pom文件引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
新建webservice接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @Title: WebServiceDemoService.java
* @Description: TODO()
* @Author: 愛飄de小子 上午9:39
* @Date: 2018年10月26日 09點(diǎn)39分
*/
@WebService
public interface WebServiceDemoService {
@WebMethod
String hello(@WebParam(name = "name")String name);
}
接口的實(shí)現(xiàn)類
import org.springframework.stereotype.Service;
import javax.jws.WebService;
/**
* @Title: WebServiceDemoServiceImpl.java
* @Description: TODO()
* @Author: 愛飄de小子 上午9:39
* @Date: 2018年10月26日 09點(diǎn)39分
*/
@Service
@WebService(serviceName = "WebServiceDemoService", // 與接口中指定的name一致
targetNamespace = "http://webservice.business.mixpay.com", // 與接口中的命名空間一致,一般是接口的包名倒
endpointInterface = "com.mixpay.business.webservice.WebServiceDemoService" // 接口地址
)
public class WebServiceDemoServiceImpl implements WebServiceDemoService {
@Override
public String hello(String name) {
return "hello"+name;
}
}
新建webservice配置類WebServiceConfig
import com.mixpay.business.webservice.WebServiceDemoService;
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.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;
/**
* @Title: WebServiceConfig.java
* @Description: TODO(WebService配置)
* @Author: 愛飄de小子 16:13
* @Date: 2018年08月03日 16點(diǎn)13分
*/
@Configuration
public class WebServiceConfig {
@Autowired
private WebServiceDemoService webServiceDemoService;
/**
* 注入servlet bean name不能dispatcherServlet 否則會覆蓋dispatcherServlet
* @return
*/
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 注冊WebServiceDemoService接口到webservice服務(wù)
* @return
*/
@Bean(name = "WebServiceDemoEndpoint")
public Endpoint sweptPayEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceDemoService);
endpoint.publish("/webservice");
return endpoint;
}
}
啟動項(xiàng)目后,游覽器訪問 http://localhost:項(xiàng)目端口/CXFServlet注冊地址 本例訪問http://localhost:8500/webservice
點(diǎn)擊紅色部分查看wsdl服務(wù)描述文檔
wsdl文檔地址為: http://localhost:端口/CXFServlet注冊地址/接口注冊地址?wsdl
本例訪問:http://localhost:8500/webservice/webservice?wsdl
至此害碾,webservice服務(wù)提供已經(jīng)完成矢劲,接下來,webservice接口的調(diào)用
在idea中右鍵點(diǎn)擊項(xiàng)目 --> webservices --> Generate Java Code From Wsdl
點(diǎn)擊ok慌随,會在相應(yīng)的包下生成芬沉。java文件和class文件
測試: