准备工作:
创建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();
}
}
}
|
嗯…总体上就是这么简单
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/weixin_41138656/article/details/79393366
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 90
-
2025-06-04 32
-
2025-05-25 50
-
2025-05-25 31
-
2025-05-29 89
热门评论


