生活随笔
收集整理的這篇文章主要介紹了
SpringBoot+webservice
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天看到一個項目要和工廠的ERP進行對接,用到了webservice。雖然使用用springboot較為方便,還是了解一下:
webservice是什么
網上的解釋很多,其實就是跨語言和操作系統的的遠程調用技術。比如亞馬遜,可以將自己的服務以webservice的服務形式暴露出來,我們就可以通過web調用這些,無論我們使用的語言是java還是c,這也是SOA應用一種表現形式。
WSDL(Web Services Description Language)將無論用何種語言書寫的web service描述出來,比如其參數或返回值。WSDL是服務端和客戶端都能解讀的標準格式??蛻舳送ㄟ^URL地址訪問到WSDL文件,在調用服務端之前先訪問WSDL文件。 讀取到WSDL后通過客戶端的API類可以生成代理類,調用這些代理類就可以訪問webservice服務。代理類將客戶端的方法變為soap(Simple Object Access Protocol,可以理解為http+xml)格式通過http發送,同時接受soap格式的返回值并解析。
webservice使用
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.41</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.1.11</version></dependency><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>2.11.0</version></dependency></dependencies>
復制代碼 public class TestBean {
private String name;
private String id;
public String getName() {
return name;}
public void setName(String name) {
this.name = name;}
public String getId() {
return id;}
public void setId(String id) {
this.id = id;}}
復制代碼 @WebService(name =
"testService",targetNamespace =
"http://service.webservicedemo.sxt.com")
public interface Wbservice {
@WebMethod@WebResult()
public String helloService(@WebParam(name = "name") String name);
@WebMethodpublic ArrayList<TestBean> getAllBean()}
復制代碼 @WebService(name =
"testService",targetNamespace =
"http://service.webservicedemo.sxt.com",endpointInterface=
"com.sxt.webservicedemo.Service.Wbservice")
@Componentpublic class WebserviceImpl implements Wbservice {
@Overridepublic String helloService(String name) {
return "hello"+name;}
@Overridepublic ArrayList<TestBean> getAllBean() {ArrayList<TestBean> list =
new ArrayList<>();TestBean bean1 =
new TestBean(
"zhangsan",
"1");TestBean bean2 =
new TestBean(
"lisi",
"2");list.add(bean1);list.add(bean2);
return list;}}
復制代碼@Configuration
public class Webserviceconfig {
@Beanpublic ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(
new CXFServlet(),
"/service/*");}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();}
@Beanpublic Wbservice beanService(){
return new WebserviceImpl();}
@Beanpublic Endpoint endpoint() {EndpointImpl endpoint=
new EndpointImpl(springBus(), beanService());endpoint.publish(
"/test");
return (Endpoint) endpoint;}
復制代碼public static void main(String[] args) throws Exception {JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();Client client=dcflient.createClient(
"http://localhost:8080/service/test?wsdl");Object[] objects=client.invoke(
"getBean",
"411001");System.out.println(
"*******"+objects[
0].toString());}
復制代碼
轉載于:https://juejin.im/post/5cbb09666fb9a06856568ae5
總結
以上是生活随笔為你收集整理的SpringBoot+webservice的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。