准备工作:
创建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