Web Services開發
==============
常用的 Web Services 框架有 Apache Axis1 、 Apache Axis2 、 Apache CXF ,而 Apache Axis1 已經逐漸被淘汰所以本文不會討論,重點關注 Apache Axis2 及 Apache CXF 。
Apache Axis2
============
在IDEA中新建 Axis2Demo 項目后右鍵選擇?添加框架的支持?并選中 Web Application 。
從Apache Axis2官網處下載 war包 進行部署,將 axis2.war 解壓后把 WEB-INF 和 axis2-web目錄復制到項目的 web 目錄下(如下圖所示)并啟動Tomcat Server。
訪問
http://localhost:8080/Axis2Demo_war_exploded/axis2-web/index.jsp 出現下圖的頁面表示部署成功。
?
Axis2配置
=======
在 Axis1 中的全局配置和 Servcies 的配置均在 server-config.wsdd 中進行配置,而 Axis2則將全局配置單獨存放于 WEB-INF/conf/axis2.xml 中, services 的配置文件則位于 servcies。
發布服務(Publish Service)
=====================
新建一個 HelloService 類并編譯為 HelloService.class 復制至 WEB-INF/pojo 目錄下并重啟服務。
// 不能聲明package
public class HelloService {
public HelloService(){}
public String sayHello() {
return “hello”;
}
public String sayHelloToPerson(String name) {
if (name == null) {
name = “nobody”;
}
return "hello, " + name;
}
}
重啟服務后再次訪問
http://localhost:8080/Axis2Demo_war_exploded/services/HelloService?wsdl 即可發現新發布的服務,點擊 HelloService 即可查看Axis自動為該服務生成的WSDL,其描述了如何調用服務的方法及返回內容:
使用 SoapUI 客戶端調用 HelloService 服務方法:
?
而之所以 WEB-INF/pojo 目錄下的 .class 文件會自動發布為服務是因為在 axis2.xml 配置文件中的 deployer 標簽中所配置的該選項。
上述的方式發布服務需要將編譯后的類放置在某個具體的目錄中,且不能包含 package ,而使用 *.aar 的方式則可以解決此問題。首先在Project的根目錄下新建 META-INF/services.xml ,文件內容可以參考官方示例 version.aar 。
<?xml version="1.0" encoding="UTF-8"?>一個簡單的WebService
com.ws.test.services.HelloService
<messageReceiver mep=“http://www.w3.org/ns/wsdl/in-only”
class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />
<messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out”
class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
最終結構為如下所示,在項目根目錄中執行?jar cvf HelloService.aar .?進行打包。
?
將打包后的文件復制至 WEB-INF/services 目錄下,即可在服務列表中看到新注冊的服務,或者在 Axis 后臺中也可以上傳包部署(因此如果應用程序的Axis后臺可訪問且為默認憑據即可部署惡意Service獲取權限)。
?
客戶端服務調用
=======
調用 Web Service 可通過代碼的方式實現也可以通過WSDL構造SOAP協議調用方法,最簡便的方法則是使用SoapUI,其會根據 Web Service 的WSDL生成對應方法的SOAP協議請求。
// 代碼實現Web Service調用
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class WebServiceClient {
public static void main(String[] args) throws Exception {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(“http://192.168.0.105:8080/Axis2Demo_war_exploded/services/HelloService”);
options.setTo(targetEPR);
Object[] entryArgs = new Object[]{4, 2};
QName qName = new QName(“http://ws.apache.org/axis2”, “add”);
Object result = serviceClient.invokeBlocking(qName, entryArgs, new Class[]{int.class})[0];
qName = new QName(“http://ws.apache.org/axis2”, “send”);
serviceClient.invokeRobust(qName, new Object[]{“hello world!”});
}
}
Soap UI
?
Apache CXF
==========
Apache CXF是一個開源的、全功能的,容易使用的Web服務框架。CXF是兩個項目的結合:由IONA技術公司開發的Celtix和由Codehaus主持的團隊開發的XFire。
CXF支持的特性非常廣泛,但特性主要在以下一些方面:
-
支持的Web服務標準包括: SOAP WS-Addressing WS-Policy WS-ReliableMessaging WS-Security WS-SecurityPolicy WS-SecureConversation
-
JAS-WS API,用于Web服務開發 WSDL優先支持工具 Java優先支持
-
JAX-RS(JSR 311 1.0)API,用于RESTful Web服務開發
-
…
??內容摘自Wiki百科。
發布服務
====
使用 Maven 構建項目,POM文件內容如下:
<?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”>
4.0.0
org.example
CXFDemo
1.0-SNAPSHOT
org.apache.cxf
cxf-rt-frontend-jaxws
3.4.0
org.apache.cxf
cxf-rt-transports-http
3.4.0
org.apache.cxf
cxf-rt-transports-http-jetty
3.4.0
org.apache.cxf
cxf-rt-transports-http-jetty
3.4.0
編寫一個服務接口,定義 sayHi 方法:
package org.example.services;
import javax.jws.WebService;
// 聲明這是一個Ws服務接口
@WebService
public interface HelloWorld {
// 定義服務方法
String sayHi(String name);
}
編寫一個服務接口的實現類:
package org.example.services;
import javax.jws.WebService;
@WebService(endpointInterface = “org.example.services.HelloWorld”, serviceName = “HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String name) {
return "hi, " + name;
}
}
再編寫一個發布服務的主類 Main :
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.example.services.HelloWorld;
import org.example.services.HelloWorldImpl;
public class Main {
最后
總的來說,面試官要是考察思路就會從你實際做過的項目入手,考察你實際編碼能力,就會讓你在電腦敲代碼,看你用什么編輯器、插件、編碼習慣等。所以我們在回答面試官問題時,有一個清晰的邏輯思路,清楚知道自己在和面試官說項目說技術時的話就好了
pl implements HelloWorld {
public String sayHi(String name) {
return "hi, " + name;
}
}
再編寫一個發布服務的主類 Main :
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.example.services.HelloWorld;
import org.example.services.HelloWorldImpl;
public class Main {
最后
總的來說,面試官要是考察思路就會從你實際做過的項目入手,考察你實際編碼能力,就會讓你在電腦敲代碼,看你用什么編輯器、插件、編碼習慣等。所以我們在回答面試官問題時,有一個清晰的邏輯思路,清楚知道自己在和面試官說項目說技術時的話就好了
[外鏈圖片轉存中…(img-YIdcWISY-1720090990881)]
[外鏈圖片轉存中…(img-ScyT4msQ-1720090990882)]