Springboot整合cxf进行WebService发布和WebService调用
【摘要】 1:添加依赖 Maven工程:
<dependency>
<groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.3</version>...
1:添加依赖
Maven工程:
<dependency>
<groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.3</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
Gradle工程:
'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.3.3'
- 1
2:服务端接口
package com.nobody.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* 测试接口
*
* @author Μr.ηobοdy
*
* @date 2019-12-29
*
*/
@WebService(name = "UserService", // 暴露服务名称 targetNamespace = "http://service.nobody.com" // 命名空间,一般是接口的包名倒序
)
public interface UserService { @WebMethod @WebResult(name = "String", targetNamespace = "") String addUser(@WebParam(name = "username") String username, @WebParam(name = "age") int age);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3:服务端接口实现
package com.nobody.service.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.nobody.service.UserService;
/**
* 测试接口实现
*
* @author Μr.ηobοdy
*
* @date 2019-12-29
*
*/
@WebService(serviceName = "UserService", // 与接口中指定的name一致 targetNamespace = "http://service.nobody.com", // 与接口中的命名空间一致,一般是接口的包名倒 endpointInterface = "com.nobody.service.UserService" // 接口地址
)
@Component
public class UserServiceImpl implements UserService { @Override public String addUser(String username, int age) { return "Add user success,username:" + username + ",age:" + age; }
}
- 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
4:CXF配置
package com.nobody.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nobody.service.UserService;
/**
* cxf配置
*
* @author Μr.ηobοdy
*
* @date 2019-12-29
*
*/
@Configuration
public class CxfConfig { @Autowired private Bus bus; @Autowired private UserService userService; @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/UserService"); return endpoint; }
}
- 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
5:wsdl文件
默认服务在host:port/工程服务名/services/所在的路径下。因为本工程没设置服务名,故此接口发布路径为/services/UserService,wsdl文件路径为:
http://localhost:8080/services/UserService?wsdl
- 1
服务启动后,在浏览器输入以上地址即可看到wsdl文件:
6:客户端调用
package com.nobody.controller;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("demo")
public class DemoController { @GetMapping("test") public String test(@RequestParam String username, @RequestParam int age) { String result = null; // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/services/UserService?wsdl"); // 如果需要密码时加上用户名和密码 // client.getOutInterceptors().add(new ClientLoginInterceptor(USERNAME,PASSWORD)); // 接受返回值对象 Object[] objects = new Object[0]; try { // 调用 objects = client.invoke("addUser", username, age); result = "调用webservice接口返回数据:" + objects[0]; } catch (Exception e) { e.printStackTrace(); } return result; }
}
- 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
7:启动工程,在浏览器输入以下地址即可进行调用
http://127.0.0.1:8080/demo/test?username=Mr.nobody&age=18
- 1
返回结果:
8:项目Github下载地址
Springboot整合cxf使用WebService
文章来源: javalib.blog.csdn.net,作者:陈皮的JavaLib,版权归原作者所有,如需转载,请联系作者。
原文链接:javalib.blog.csdn.net/article/details/103758269
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)