Spring Boot+CXF搭建WebService
Spring Boot WebService开发

需要依赖Maven的Pom清单
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath/>
</parent>
<groupId>com.mracale</groupId>
<artifactId>webservice</artifactId>
<version>0.0.-SNAPSHOT</version>
<name>webservice</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--WerbService CXF依赖-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3. 开始编写webService服务端
3.1 实体类entity
package com.mracale.springboot.Entity; import java.io.Serializable; /**
*用户实体类
*/
public class User implements Serializable {
private static final long serialVersionUID = -3628469724795296287L; private String userId;
private String userName;
private String email; public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", email='" + email + '\'' +
'}';
}
}
3.2 服务接口
package com.mracale.springboot.Service; import com.mracale.springboot.Entity.User; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; //@WebService(targetNamespace="http://service.springboot.mracale.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知.
@WebService(targetNamespace="http://service.springboot.mracale.com")
public interface UserService { @WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
public User getUser(@WebParam(name = "userId") String userId); @WebMethod
@WebResult(name="String",targetNamespace="")
public String getUserName(@WebParam(name = "userId") String userId); }
3.3 服务接口的实现类
package com.mracale.springboot.Service.impl; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import javax.jws.WebService; import com.mracale.springboot.Entity.User;
import com.mracale.springboot.Service.UserService;
import org.springframework.stereotype.Component; /**
* @ClassName:UserServiceImpl
* @Description:测试服务接口实现类
*/
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.springboot.mracale.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.mracale.springboot.Service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@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.setUserName("mracale01");
user.setEmail("mracale01@163.xom");
userMap.put(user.getUserId(), user); user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("mracale02");
user.setEmail("mracale02@163.xom");
userMap.put(user.getUserId(), user); user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("mracale03");
user.setEmail("mracale03@163.xom");
userMap.put(user.getUserId(), user);
}
@Override
public String getUserName(String userId) {
return "userId为:" + userId;
}
@Override
public User getUser(String userId) {
System.out.println("userMap是:"+userMap);
return userMap.get(userId);
} }
3.4 发布webService的配置
package com.mracale.springboot.config; import com.mracale.springboot.Service.UserService;
import com.mracale.springboot.Service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration
public class CxfConfig { /**
* 此方法作用是改变项目中服务名的前缀名,此处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
* @return
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
} @Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus()
{
return new SpringBus();
} @Bean
public UserService userService()
{
return new UserServiceImpl();
} @Bean
public Endpoint endpoint() {
EndpointImpl endpoint=new EndpointImpl(springBus(), userService());//绑定要发布的服务
endpoint.publish("/user"); //显示要发布的名称
return endpoint;
}
}
4. 项目启动后的wsdl信息

5. 两种调用方式
package com.mracale.springboot.client; import com.mracale.springboot.Service.UserService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; /**
* @ClassName:CxfClient
* @Description:webservice客户端:
* 该类提供两种不同的方式来调用webservice服务
* 1:代理工厂方式
* 2:动态调用webservice
*/
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:8080/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:8080/soap/user?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getUserName", "maple");
System.out.println("返回数据:" + objects[]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
6. 注意点.
诚如之前所说,如果接口的注解上不加targetNamespace的话,动态调用的时候,会报如下的错误。
Spring Boot+CXF搭建WebService的更多相关文章
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
- Spring Boot+CXF搭建WebService服务参考资料
pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
- Spring boot项目搭建及简单实例
Spring boot项目搭建 Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for ...
- spring boot+mybatis搭建项目
一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
随机推荐
- ubuntu zip解压
您好,zip xx.zip压缩,unzip xx.zip 解压,tar zcvf xx.tar.gz压缩tar zxvf xx.tar.gz解压
- listView解决滑动时黑色背景问题
listView.setCacheColorHint(Color.TRANSPARENT);//解决滑动时黑色背景问题 listView滑动时黑色背景问题 原因在于ListView存在缓存颜色机制,因 ...
- Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!
Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...
- eclipse不能自动编译XX.java为XX.classs
问题描述:eclipse不能自动编译XX.java为XX.classs 原因:今天下午写代码,因为需要引入jstl包,引入后发现原来项目中已经引入了,然后我又把包删除了,忘记删除java build ...
- list中的比较
一说到list的的确不知道写些什么.....我觉得别人总结的比我写的还要好很多. 我擅长记录自己的误区. |--List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3 ...
- RF使用的相关库API
RF内置库: http://robotframework.org/robotframework/ SSHLibrary: ---WEB自动化测试 http://robotframework.org ...
- 浅谈Nutch插件机制(含开发实例)
plugin(插件)为nutch提供了一些功能强大的部件,举个例子,HtmlParser就是使用比较普遍的用来分析nutch抓取的html文件的插件. 为什么nutch要使用这样的plugin系统? ...
- Kconfig和Makefile的修改
Kconfig文件的作用 内核源码树的目录下都有两个文件Kconfig(2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconf ...
- delphi中Case语法的使用方法
Case 语句If...Then…Else 语句适合选项较少的情况,如果有很多选项的话利用If 语句就比较麻烦,在这种情况下,Case 语句就容易多了.Case 语句的语法如下: case <表 ...
- CSS-项目中遇到IE兼容问题,处理随笔
总是忘记给ie做特殊样式处理,以前打游击,不做也就算了,以后可不行,得对自己的“孩子”负责.. 一.先说IE老大的兼容 知道了一些常用的css属性兼容方法确实可以解决问题, 但我不知道我自己的ieTe ...
