springboot整合cxf發(fā)布webservice以及調(diào)用的方法
webservice性能不高,但是現(xiàn)在好多公司還是在用,恰好今天在開(kāi)發(fā)的時(shí)候?qū)禹?xiàng)目組需要使用到webservice下面來(lái)說(shuō)下簡(jiǎn)單的案例應(yīng)用
首先老規(guī)矩:引入jar包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.11</version> </dependency>
新增一個(gè)公共的返回實(shí)體類(lèi)
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;
}
}
其他類(lèi)繼承即可
@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類(lèi)
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;
}
}
接下來(lái)新增服務(wù)接口
@WebService
public interface TestWService{
@WebMethod
@WebResult
TestResult list3();
}
實(shí)現(xiàn)服務(wù)接口
@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;
}
}
新增配置類(lèi),發(fā)布服務(wù)
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ā)布服務(wù)類(lèi)
* @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() {
//會(huì)找到O2oWebService的實(shí)現(xiàn)類(lèi),所以實(shí)現(xiàn)類(lèi)只能有一個(gè)
EndpointImpl endpoint = new EndpointImpl(springBus(), testWService);
endpoint.publish("/testWService");
return endpoint;
}
}
啟動(dòng)項(xiàng)目,然后打開(kāi)路徑:localhost:8080/soap 可以查看多個(gè)自己發(fā)布的服務(wù),如果要發(fā)布多個(gè)服務(wù),使用多個(gè)Bean即可
測(cè)試調(diào)用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ù)
//輸出調(diào)用結(jié)果
System.out.println(objects[0].getClass());
System.out.println(objects[0].toString());
}
}
客戶(hù)端調(diào)用,用soapUI生成客戶(hù)端(具體方法自己百度下,不介紹了)
TestWSImplService implService = new TestWSImplService (); TestServiceWS ws = implService.getTestServiceWSImplPort(); TestResult result = ws.list3(); System.err.println(result);
增加密碼校驗(yàn),以下基礎(chǔ)內(nèi)容引用http://www.dbjr.com.cn/article/145707.htm,我補(bǔ)充下包依賴(lài)
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) {
//設(shè)置在發(fā)送請(qǐng)求前階段進(jìn)行攔截
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() {
//定義在哪個(gè)階段進(jìn)行攔截
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,無(wú)法驗(yàn)證用戶(hù)信息"));
}
//獲取用戶(hù)名,密碼
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("用戶(hù)信息為空"));
}
}
//校驗(yàn)用戶(hù)名密碼
if(!(username.equals(USERNAME) && password.equals(PASSWORD))){
SOAPException soapExc = new SOAPException("認(rèn)證失敗");
throw new Fault(soapExc);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類(lèi)型)
- SpringBoot項(xiàng)目使用?axis?調(diào)用webservice接口的實(shí)踐記錄
- webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對(duì)象傳遞示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例
- springboot調(diào)用webservice-soap接口的實(shí)現(xiàn)
- springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
- SpringBoot調(diào)用WebService接口方法示例代碼
相關(guān)文章
SpringBoot集成企業(yè)微信開(kāi)發(fā)的實(shí)現(xiàn)
本文將詳細(xì)介紹如何使用?Spring?Boot?集成企業(yè)微信開(kāi)發(fā),通過(guò)企業(yè)微信?API?可以實(shí)現(xiàn)企業(yè)內(nèi)部的一些自動(dòng)化業(yè)務(wù)流程,提高工作效率,感興趣的可以了解一下2023-07-07
springBoot+dubbo+zookeeper實(shí)現(xiàn)分布式開(kāi)發(fā)應(yīng)用的項(xiàng)目實(shí)踐
本文主要介紹了springBoot+dubbo+zookeeper實(shí)現(xiàn)分布式開(kāi)發(fā)應(yīng)用的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
三種SpringBoot中實(shí)現(xiàn)異步調(diào)用的方法總結(jié)
Spring Boot 提供了多種方式來(lái)實(shí)現(xiàn)異步任務(wù),這篇文章主要為大家介紹了常用的三種實(shí)現(xiàn)方式,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05
Java垃圾回收機(jī)制的finalize方法實(shí)例分析
這篇文章主要介紹了Java垃圾回收機(jī)制的finalize方法,結(jié)合實(shí)例形式分析了finalize方法的特點(diǎn)及在垃圾回收機(jī)制中的相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
IntelliJ?IDEA快速查詢(xún)maven依賴(lài)關(guān)系圖文教程
Maven提供了來(lái)查看依賴(lài)關(guān)系,而IDE往往提供了更加便利的方式,比如Eclipse或者IDEA都有類(lèi)似的功能,下面這篇文章主要給大家介紹了關(guān)于IntelliJ?IDEA快速查詢(xún)maven依賴(lài)關(guān)系的相關(guān)資料,需要的朋友可以參考下2023-11-11
Java使用組件編寫(xiě)窗口實(shí)現(xiàn)網(wǎng)絡(luò)圖片顯示
這篇文章主要為大家詳細(xì)介紹了Java使用組件編寫(xiě)窗口實(shí)現(xiàn)網(wǎng)絡(luò)圖片顯示的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Java多線(xiàn)程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解
Java的synchronized關(guān)鍵字用于修飾線(xiàn)程同步,用以線(xiàn)程資源共享的目的等,下面就帶來(lái)簡(jiǎn)單的Java多線(xiàn)程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解2016-06-06
Java使用Cipher類(lèi)實(shí)現(xiàn)加密的過(guò)程詳解
這篇文章主要介紹了Java使用Cipher類(lèi)實(shí)現(xiàn)加密的過(guò)程詳解,Cipher類(lèi)提供了加密和解密的功能,創(chuàng)建密匙主要使用SecretKeySpec、KeyGenerator和KeyPairGenerator三個(gè)類(lèi)來(lái)創(chuàng)建密匙。感興趣可以了解一下2020-07-07

