SpringBoot整合cxf发布webService
1. 看看项目结构图
2. cxf的pom依赖
1 <dependency>
2 <groupId>org.apache.cxf</groupId>
3 <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
4 <version>3.2.4</version>
5 </dependency>
3. 开始编写webService服务端
3.1 实体类entity
1 package com.example.demo.entity;
2
3 import java.io.Serializable;
4 /
5 @ClassName:User
6 @Description:测试实体
7 @author Jerry
8 @date:2018年4月10日下午3:57:38
9 */
10 public class User implements Serializable{
11
12 private static final long serialVersionUID = -3628469724795296287L;
13
14 private String userId;
15 private String userName;
16 private String email;
17 public String getUserId() {
18 return userId;
19 }
20 public void setUserId(String userId) {
21 this.userId = userId;
22 }
23 public String getUserName() {
24 return userName;
25 }
26 public void setUserName(String userName) {
27 this.userName = userName;
28 }
29 public String getEmail() {
30 return email;
31 }
32 public void setEmail(String email) {
33 this.email = email;
34 }
35 @Override
36 public String toString() {
37 return "User [userId=" + userId + ", userName=" + userName + ", email=" + email + "]";
38 }
39
40 }**
3.2 服务接口
package com.example.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;import com.example.demo.entity.User;
/
- @ClassName:UserService
- @Description:测试服务接口类
- include:两个测试方法
- @author Jerry
@date:2018年4月10日下午3:58:10br/>*/
//@WebService(targetNamespace="http://service.demo.example.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知.
@WebService(targetNamespace="http://service.demo.example.com")
public interface UserService {@WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
public User getUser(@WebParam(name = "userId") String userId);}**
3.3 服务接口的实现类
package com.example.demo.service.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
/
- @ClassName:UserServiceImpl
- @Description:测试服务接口实现类
- @author Jerry
@date:2018年4月10日下午3:58:58br/>*/
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.demo.example.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.example.demo.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口br/>@Component
public class UserServiceImpl implements UserService{private Map<String, User> userMap = new HashMap<String, User>();
public UserServiceImpl() {
System.out.println("向实体类插入数据");
User user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));user.setEmail("Jerry@163.xom");<br "="" rel="nofollow">br/>user.setUserName("test1");
user.setEmail("Jerry@163.xom");
userMap.put(user.getUserId(), user);user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test2");
user.setEmail("Jerryfix@163.xom");
userMap.put(user.getUserId(), user); user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test3");
user.setEmail("Jerryfix@163.xom");
userMap.put(user.getUserId(), user);}br/>@Override
public String getUserName(String userId) {
return "userId为:" + userId;br/>}
@Override
public User getUser(String userId) {
System.out.println("userMap是:"+userMap);
return userMap.get(userId);
}}****
3.4 发布webService的配置
package com.example.demo.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.example.demo.service.UserService;
/**
- @ClassName:CxfConfig
- @Description:cxf发布webservice配置
- @author Jerry
@date:2018年4月10日下午4:12:24br/>*/
@Configuration
public class CxfConfig {br/>@Autowired
private Bus bus;@Autowired
UserService userService;/**
- 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
- 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
- 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
- @returnbr/>*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}/** JAX-WS
- 站点服务
}
4. 项目启动后的wsdl信息

由于图省事,我将项目的服务端口改为了80,这样就省去了IP后面写端口号的麻烦。
5. 两种调用方式
package com.example.demo.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.example.demo.service.UserService;
/**
- @ClassName:CxfClient
- @Description:webservice客户端:
- 该类提供两种不同的方式来调用webservice服务
- 1:代理工厂方式
- 2:动态调用webservice
- @author Jerry
@date:2018年4月10日下午4:14:07
*/
public class CxfClient {public static void main(String[] args) {
CxfClient.main1();
CxfClient.main2();
}/**
- 1.代理类工厂的方式,需要拿到对方的接口地址
*/
public static void main1() {
try {
// 接口地址
String address = "http://127.0.0.1/soap/user?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
// 创建一个代理接口实现
UserService us = (UserService) jaxWsProxyFactoryBean.create();
// 数据准备
String userId = "maple";
// 调用代理接口的方法调用并返回结果
String result = us.getUserName(userId);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}/**
- 2:动态调用
*/
public static void main2() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1/soap/user?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getUserName", "maple");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
6. 注意点.
诚如之前所说,如果接口的注解上不加targetNamespace的话,动态调用的时候,会报如下的错误。

SpringBoot整合cxf发布webService的更多相关文章
- SpringMVC4整合CXF发布WebService
SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...
- spring-boot整合Cxf的webservice案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 <?xml version="1.0&q ...
- SpringBoot2.1.6 整合CXF 实现Webservice
SpringBoot2.1.6 整合CXF 实现Webservice 前言 最近LZ产品需要对接公司内部通讯工具,采用的是Webservice接口.产品框架用的SpringBoot2.1.6,于是采用 ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型
在昨天研究了发布CXF发布webservice之后想着将以前的项目发布webservice接口,可是怎么也发布不起来,服务启动失败,原来是自己的接口有返回值类型是Map. 研究了一番之后,发现: we ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
- CXF整合Spring发布WebService实例
一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...
- Spring整合CXF发布及调用WebService
这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法 添加jar包 (官方下载地址:http://cxf.apache.org/downlo ...
- spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)
spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...
随机推荐
- SpringMVC没有接受到参数的坑
其实说上来也不是SpringMVC的坑. 相同的一份代码,我在windows上用mvn打成jar放到linux上执行,POST请求可以接收到参数: 但是我直接在linux上从git拉取分支,并在lin ...
- [4G]4G模块的热重启
最近在调试4G模块,发现在开机启动时执行的AT指令会概率性的出现返回杂乱字符串的问题.想尽了各种办法还是行不通,在系统中使用minicom敲AT指令就不会有问题,开始怀疑是串口初始化的问题,修改了很多 ...
- hive表增量抽取到oracle数据库的通用程序(二)
hive表增量抽取到oracle数据库的通用程序(一) 前一篇介绍了java程序的如何编写.使用以及引用到的依赖包.这篇接着上一篇来介绍如何在oozie中使用该java程序. 在我的业务中,分为两段: ...
- 15.01.23-sql的注入式攻击
很多网站上有登录和忘记密码的链接,可能存在sql注入的隐患.在忘记密码(把密码发送到邮箱)那里测试. 获取数据 1.'的妙用.在邮箱栏输入emailaddress',如果返回服务器错误,则说明sql注 ...
- 再谈git的http服务
因为git服务器搬迁,需要重新安装git服务器,在网上搜索了下,发现之前的方法太复杂,复杂到自己都没彻底弄明白.其实通过git自带的git-http-backend脚本配合apache2的http服务 ...
- jquery操作select取值赋值与设置选中[转]
本节内容:jquery实现select下拉框的取值与赋值,设置选中的方法大全. 比如<select class="selector"></select> 1 ...
- FreeRDP的安装配置(错误信息:SSL_read: Failure in SSL library (protocol error?))
最新文章:Virson's Blog 使用xfreerdp [serveripaddress]命令,连接xp/windows 2003都正常,但是在连接win7/2008时总是出错: ;------- ...
- Error:(1, 0) Plugin with id 'com.android.application' not found
Error:(1, 0) Plugin with id 'com.Android.application' not found.Open File 这个错误是build.gradle造成的,我们打开文 ...
- USB学习笔记连载(十一):CY7C68013A的启动方式-EEPROM
上述的应用笔记中有介绍FX2LP的启动选项,主要包括I2C启动和USB启动. 说白了I2C启动需要使用外部的EEPROM,USB启动,只是使用上位机控制软件将配置程序FX2LP中,不用EEPRO ...
- Linux之统计特定进程运行数量
比如统计用户名为albert运行python的进程数目 ps -u albert | grep -c "python"