在開發過程中遇到了一些使用webservice開發的需求,后查閱資料學習,可上手開發。在學習過程中實現了個小demo,為了養成良好的總結習慣(我還沒這”壞習慣“),特意寫了個小呆萌,記錄一下搭建過程。
-
首先導入必需jar(pom.xml)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.1.2.RELEASE</version>-->
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>springboot-cxf-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-cxf-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
新建測試服務
UserService.java
package com.demo.springbootcxfdemo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
//指定webservice命名空間
@WebService(targetNamespace = "http://service.springbootcxfdemo.demo.com")
public interface UserService {
@WebMethod(operationName = "getHello")//在wsdl文檔中顯示的方法名,可不指定默認與方法相同;@WebMethod可不指定
String getHello(@WebParam(name = "name") String name);
}
UserServiceImpl.java
package com.demo.springbootcxfdemo.service.impl;
import com.demo.springbootcxfdemo.service.UserService;
import org.springframework.stereotype.Component;
/**
* @Author Guixing
* @Date 2019/1/17 14:00
* @Description
*/
@Component
public class UserServiceImpl implements UserService {
@Override
public String getHello(String name) {
return "hello "+name;
}
}
-
配置服務訪問路徑並發布服務
package com.demo.springbootcxfdemo.config;
import com.demo.springbootcxfdemo.service.UserService;
import org.apache.cxf.Bus;
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;
/**
* @Author Guixing
* @Date 2019/1/17 14:01
* @Description
*/
@Configuration
public class CXFConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
/**
* 此方法被注釋后:wsdl訪問地址為http://127.0.0.1:8080/services/user?wsdl
* 去掉注釋后:wsdl訪問地址為:http://127.0.0.1:8080/soap/user?wsdl
*/
@Bean
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/soap/*");
}
/**
* 發布服務
* 指定訪問url
* @return
*/
@Bean
public Endpoint userEndpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,userService);
endpoint.publish("/user");
return endpoint;
}
}
-
測試
瀏覽器中查看
可以在瀏覽器中輸入http://localhost:8080/soap/user?wsdl來查看
紅線圈出為可查看詳情的url:http://localhost:8080/soap/user?wsdl=UserService.wsdl
java客戶端訪問
CXFClient.java
package com.demo.springbootcxfdemo.client;
import com.demo.springbootcxfdemo.service.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
* @Author Guixing
* @Date 2019/1/17 14:53
* @Description
*/
public class CXFClient {
public static void main(String[] args) {
try {
// 接口地址
String address = "http://127.0.0.1:8080/soap/user?wsdl";
// 代理工廠
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 設置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 設置接口類型
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
// 創建一個代理接口實現
UserService us = (UserService) jaxWsProxyFactoryBean.create();
// 數據准備
String name = "zhang";
// 調用代理接口的方法調用並返回結果
String result = us.getHello(name);
System.out.println("返回結果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
postman測試
請求中的name標簽即為@WebParam中name指定的名稱,下面為后端返回數據。
寫在最后
到這里一個簡單的springboot整合cxf框架開發webservice服務的demo就開發完了。
源碼地址:https://github.com/zhangguixing/springboot-demo
學習使我快樂!😀