您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關springboot如何整合cxf發布webservice的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
webservice性能不高,但是現在好多公司還是在用,恰好今天在開發的時候對接項目組需要使用到webservice下面來說下簡單的案例應用
首先老規矩:引入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(); }
實現服務接口
@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; } }
新增配置類,發布服務
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 發布服務類 * @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的實現類,所以實現類只能有一個 EndpointImpl endpoint = new EndpointImpl(springBus(), testWService); endpoint.publish("/testWService"); return endpoint; } }
啟動項目,然后打開路徑:localhost:8080/soap 可以查看多個自己發布的服務,如果要發布多個服務,使用多個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方法名 后面是可變參數 //輸出調用結果 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);
增加密碼校驗,以下基礎內容引用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) { //設置在發送請求前階段進行攔截 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如何整合cxf發布webservice”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。