溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot如何整合cxf發(fā)布webservice

發(fā)布時間:2021-07-08 11:30:04 來源:億速云 閱讀:293 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)springboot如何整合cxf發(fā)布webservice的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

webservice性能不高,但是現(xiàn)在好多公司還是在用,恰好今天在開發(fā)的時候?qū)禹椖拷M需要使用到webservice下面來說下簡單的案例應(yīng)用

首先老規(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;
 } 
}

接下來新增服務(wù)接口

@WebService
public interface TestWService{
 @WebMethod
 @WebResult
 TestResult list3();
}

實現(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;
 }

}

新增配置類,發(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ù)類
 * @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ā)布的服務(wù),如果要發(fā)布多個服務(wù),使用多個Bean即可

測試調(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());
 }
}

客戶端調(diào)用,用soapUI生成客戶端(具體方法自己百度下,不介紹了)

TestWSImplService implService = new TestWSImplService ();
 TestServiceWS ws = implService.getTestServiceWSImplPort();
 TestResult result = ws.list3();
 System.err.println(result);

增加密碼校驗,以下基礎(chǔ)內(nèi)容引用https://www.jb51.net/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) {
  //設(shè)置在發(fā)送請求前階段進(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() {
  //定義在哪個階段進(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,無法驗證用戶信息"));
  }
  //獲取用戶名,密碼
  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("認(rèn)證失敗");
   throw new Fault(soapExc);
  }
 }
}

感謝各位的閱讀!關(guān)于“springboot如何整合cxf發(fā)布webservice”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI