springboot整合cxf發(fā)布webservice以及調用的方法
webservice性能不高,但是現(xiàn)在好多公司還是在用,恰好今天在開發(fā)的時候對接項目組需要使用到webservice下面來說下簡單的案例應用
首先老規(guī)矩:引入jar包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.11</version> </dependency>
新增一個公共的返回實體類
public class BaseResult { private String isSuccess; private String errCode; private String message; public String getIsSuccess() { return isSuccess; } public void setIsSuccess(String isSuccess) { this.isSuccess = isSuccess; } public String getErrCode() { return errCode; } public void setErrCode(String errCode) { this.errCode = errCode; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
其他類繼承即可
@XmlRootElement(name = "testResult") public class TestResult extends BaseResult implements Serializable { private static final long serialVersionUID = -7128575337024823798L; private List<User> data; public List<User> getData() { return data; } public void setData(List<User> data) { this.data = data; } }
新增user類
public class User { private String name; private int age; public User(String name, int age) { super(); this.name = name; this.age = age; } public User() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
接下來新增服務接口
@WebService public interface TestWService{ @WebMethod @WebResult TestResult list3(); }
實現(xiàn)服務接口
@Service @WebService(targetNamespace = "http://ws.**.com/",//命名空間,一般是接口的包名倒序) endpointInterface = "com.**.ws.TestWSservice")//接口全路徑 //**自己改自己的包路徑 public class TestWSservice Impl implements TestWSservice { @Override public TestResult list3() { List<User> list = new ArrayList<User>(); list.add(new User("張三",23)); list.add(new User("李四",24)); TestResult testResult = new TestResult(); testResult.setIsSuccess("Y"); testResult.setData(list); testResult.setMessage("操作成功"); return testResult; } }
新增配置類,發(fā)布服務
import javax.xml.ws.Endpoint; 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 com.hikvision.hikserviceassign.ws.MonitorSurveyWS; import com.hikvision.hikserviceassign.ws.SmartLockServiceOrderWS; /** * webservice 發(fā)布服務類 * @author Xupx * @Date 2018年8月14日 下午4:25:25 * */ @Configuration public class CxfConfig { @Autowired private TestWService testWService; @SuppressWarnings("all") @Bean public ServletRegistrationBean wsServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/soap/*"); return bean; } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public Endpoint testWService() { //會找到O2oWebService的實現(xiàn)類,所以實現(xiàn)類只能有一個 EndpointImpl endpoint = new EndpointImpl(springBus(), testWService); endpoint.publish("/testWService"); return endpoint; } }
啟動項目,然后打開路徑:localhost:8080/soap 可以查看多個自己發(fā)布的服務,如果要發(fā)布多個服務,使用多個Bean即可
測試調用1:
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootCxfApplicationTests { @Test public void contextLoads() throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1:8080/soap/testWservice?wsdl"); Object[] objects = client.invoke("list3",param1,param2);//list3方法名 后面是可變參數(shù) //輸出調用結果 System.out.println(objects[0].getClass()); System.out.println(objects[0].toString()); } }
客戶端調用,用soapUI生成客戶端(具體方法自己百度下,不介紹了)
TestWSImplService implService = new TestWSImplService (); TestServiceWS ws = implService.getTestServiceWSImplPort(); TestResult result = ws.list3(); System.err.println(result);
增加密碼校驗,以下基礎內容引用http://www.dbjr.com.cn/article/145707.htm,我補充下包依賴
import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; public class LoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private String username="root"; private String password="admin"; public LoginInterceptor(String username, String password) { //設置在發(fā)送請求前階段進行攔截 super(Phase.PREPARE_SEND); this.username=username; this.password=password; } @Override public void handleMessage(SoapMessage soapMessage) throws Fault { List<Header> headers = soapMessage.getHeaders(); Document doc = DOMUtils.createDocument(); Element auth = doc.createElementNS("http://cxf.wolfcode.cn/","SecurityHeader"); Element UserName = doc.createElement("username"); Element UserPass = doc.createElement("password"); UserName.setTextContent(username); UserPass.setTextContent(password); auth.appendChild(UserName); auth.appendChild(UserPass); headers.add(0, new Header(new QName("SecurityHeader"),auth)); } }
import java.util.List; import javax.xml.soap.SOAPException; import org.apache.commons.lang3.StringUtils; import org.apache.cxf.binding.soap.SoapHeader; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.apache.cxf.headers.Header; public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private static final String USERNAME="root"; private static final String PASSWORD="admin"; public AuthInterceptor() { //定義在哪個階段進行攔截 super(Phase.PRE_PROTOCOL); } @Override public void handleMessage(SoapMessage soapMessage) throws Fault { List<Header> headers = null; String username=null; String password=null; try { headers = soapMessage.getHeaders(); } catch (Exception e) { e.printStackTrace(); } if (headers == null) { throw new Fault(new IllegalArgumentException("找不到Header,無法驗證用戶信息")); } //獲取用戶名,密碼 for (Header header : headers) { SoapHeader soapHeader = (SoapHeader) header; Element e = (Element) soapHeader.getObject(); NodeList usernameNode = e.getElementsByTagName("username"); NodeList pwdNode = e.getElementsByTagName("password"); username=usernameNode.item(0).getTextContent(); password=pwdNode.item(0).getTextContent(); if( StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){ throw new Fault(new IllegalArgumentException("用戶信息為空")); } } //校驗用戶名密碼 if(!(username.equals(USERNAME) && password.equals(PASSWORD))){ SOAPException soapExc = new SOAPException("認證失敗"); throw new Fault(soapExc); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot集成企業(yè)微信開發(fā)的實現(xiàn)
本文將詳細介紹如何使用?Spring?Boot?集成企業(yè)微信開發(fā),通過企業(yè)微信?API?可以實現(xiàn)企業(yè)內部的一些自動化業(yè)務流程,提高工作效率,感興趣的可以了解一下2023-07-07springBoot+dubbo+zookeeper實現(xiàn)分布式開發(fā)應用的項目實踐
本文主要介紹了springBoot+dubbo+zookeeper實現(xiàn)分布式開發(fā)應用的項目實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03三種SpringBoot中實現(xiàn)異步調用的方法總結
Spring Boot 提供了多種方式來實現(xiàn)異步任務,這篇文章主要為大家介紹了常用的三種實現(xiàn)方式,文中的示例代碼講解詳細,需要的可以參考一下2023-05-05IntelliJ?IDEA快速查詢maven依賴關系圖文教程
Maven提供了來查看依賴關系,而IDE往往提供了更加便利的方式,比如Eclipse或者IDEA都有類似的功能,下面這篇文章主要給大家介紹了關于IntelliJ?IDEA快速查詢maven依賴關系的相關資料,需要的朋友可以參考下2023-11-11Java使用組件編寫窗口實現(xiàn)網(wǎng)絡圖片顯示
這篇文章主要為大家詳細介紹了Java使用組件編寫窗口實現(xiàn)網(wǎng)絡圖片顯示的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Java多線程編程中synchronized關鍵字的基礎用法講解
Java的synchronized關鍵字用于修飾線程同步,用以線程資源共享的目的等,下面就帶來簡單的Java多線程編程中synchronized關鍵字的基礎用法講解2016-06-06