準備工作:
創建springboot項目webservice_server
創建springboot項目webservice_client
分別添加cxf的依賴:
1
2
3
4
5
6
7
|
<!-- cxf webservice --> <dependency> <groupid>org.apache.cxf</groupid> <artifactid>cxf-spring-boot-starter-jaxws</artifactid> <version> 3.1 . 11 </version> </dependency> <!-- cxf webservice --> |
一.定義要發布的接口和實現類
接口:
1
2
3
4
5
6
7
8
9
|
@webservice public interface appservice { @webmethod string getusername( @webparam (name = "id" ) string id) throws unsupportedencodingexception; @webmethod public user getuser(string id) throws unsupportedencodingexception; } |
實現類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//name暴露的服務名稱, targetnamespace:命名空間,設置為接口的包名倒寫(默認是本類包名倒寫). endpointinterface接口地址 @webservice (name = "test" ,targetnamespace = "http://cxf.wolfcode.cn/" ,endpointinterface = "cn.wolfcode.cxf.appservice" ) public class appserviceimpl implements appservice { jsonresult jsonresult = jsonresult.getjsonresult(); @override public string getusername(string id) throws unsupportedencodingexception { system.out.println( "===========================" +id); jsonresult result= jsonresult.getjsonresult(); result.setsuccess( true ); result.setmessage( "明哥" ); return result.tojsonobject(); } @override public user getuser(string id) throws unsupportedencodingexception { system.out.println( "===========================" +id); return new user(1l, "明哥" ); } } |
二.發布服務
1.定義配置類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@configuration public class cxfconfig { //默認servlet路徑/*,如果覆寫則按照自己定義的來 @bean public servletregistrationbean dispatcherservlet() { return new servletregistrationbean( new cxfservlet(), "/services/*" ); } @bean (name = bus.default_bus_id) public springbus springbus() { return new springbus(); } //把實現類交給spring管理 @bean public appservice appservice() { return new appserviceimpl(); } //終端路徑 @bean public endpoint endpoint() { endpointimpl endpoint = new endpointimpl(springbus(), appservice()); endpoint.getininterceptors().add( new authinterceptor()); //添加校驗攔截器 endpoint.publish( "/user" ); return endpoint; } } |
2.發布服務
1
2
3
4
5
6
7
|
@springbootapplication public class webserviceapplication { public static void main(string[] args) { springapplication.run(webserviceapplication. class , args); } } |
因為我添加了用戶名和密碼校驗所以在發布之前還需要定義自己校驗用戶名和密碼的interceptor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public class authinterceptor extends abstractphaseinterceptor<soapmessage> { logger logger = loggerfactory.getlogger( this .getclass()); 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) { logger.error( "getsoapheader error: {}" ,e.getmessage(),e); } 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( "認證失敗" ); logger.debug( "用戶認證信息錯誤" ); throw new fault(soapexc); } } } |
現在可以發布服務了.....
發布完成后訪問http://localhost:8888/services/user?wsdl
能夠出現以下界面就是發布ok
三.調用服務
1.新建調用端項目,添加依賴
2.因為示例演示了兩種調用方式,其中一種需要用到接口,所以先把服務接口拷貝一份到調用端項目中(代碼就是上面接口的代碼)
3.因為服務端添加了用戶名密碼校驗,所以調用的時候需要添加用戶名密碼信息, 所以需要使用下面的interceptor完成添加用戶名密碼信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * created by sky on 2018/2/27. */ 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)); } } |
4.調用接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/** * created by sky on 2018/2/27. */ public class cxfclient { //webservice接口地址 private static string address = "http://localhost:8888/services/user?wsdl" ; //測試 public static void main(string[] args) { test1(); test2(); } /** * 方式1:使用代理類工廠,需要拿到對方的接口 */ public static void test1() { try { // 代理工廠 jaxwsproxyfactorybean jaxwsproxyfactorybean = new jaxwsproxyfactorybean(); // 設置代理地址 jaxwsproxyfactorybean.setaddress(address); //添加用戶名密碼攔截器 jaxwsproxyfactorybean.getoutinterceptors().add( new logininterceptor( "root" , "admin" ));; // 設置接口類型 jaxwsproxyfactorybean.setserviceclass(appservice. class ); // 創建一個代理接口實現 appservice cs = (appservice) jaxwsproxyfactorybean.create(); // 數據準備 string lineid = "1" ; // 調用代理接口的方法調用并返回結果 user result = (user)cs.getuser(lineid); system.out.println( "==============返回結果:" + result); } catch (exception e) { e.printstacktrace(); } } /** * 動態調用方式 */ public static void test2() { // 創建動態客戶端 jaxwsdynamicclientfactory dcf = jaxwsdynamicclientfactory.newinstance(); client client = dcf.createclient(address); // 需要密碼的情況需要加上用戶名和密碼 client.getoutinterceptors().add( new logininterceptor( "root" , "admin" )); object[] objects = new object[ 0 ]; try { // invoke("方法名",參數1,參數2,參數3....); system.out.println( "======client" +client); objects = client.invoke( "getusername" , "1" ); system.out.println( "返回數據:" + objects[ 0 ]); } catch (exception e) { e.printstacktrace(); } } } |
嗯...總體上就是這么簡單
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_41138656/article/details/79393366