SpringBoot整合CXF实例:

服务端构建

        <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
/**
* 作者信息实体
* @author oKong
*
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AuthorDto { String name; List<String> hobby; String birthday; String description; Sex sex;
}
/**
* 性别枚举类
* @author oKong
*
*/
public enum Sex { MALE("male"),
FEMALE("female"); String value; Sex(String value) {
this.value = value;
} public String value() {
return value;
} public static Sex fromValue(String v) {
for (Sex c : Sex.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
/**
* 创建服务接口
* @author oKong
*
*/
@WebService(targetNamespace = WsConst.NAMESPACE_URI ,name = "authorPortType")
public interface AuthorService { /**
* 根据名称获取作者信息
* @author 作者:oKong
*/
@WebMethod(operationName="getAuthorByName")
AuthorDto getAuthor(@WebParam(name = "authorName") String name); /**
* 获取作者列表信息
* @author oKong
*/
@WebMethod
List<AuthorDto> getAuthorList(); /**
* 返回字符串测试
* @author oKong
*/
String getAuthorString(@WebParam(name = "authorName")String name);
}
@WebService(
targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空间
name = "authorPortType", //portType名称 客户端生成代码时 为接口名称
serviceName = "authorService", //服务name名称
portName = "authorPortName", //port名称
endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定发布webservcie的接口类,此类也需要接入@WebService注解
public class AuthorServiceImpl implements AuthorService{ @Override
public AuthorDto getAuthor(String name) {
AuthorDto author = new AuthorDto();
author.setBirthday("1990-01-23");
author.setName("姓名:" + name);
author.setSex(Sex.MALE);
author.setHobby(Arrays.asList("电影","旅游"));
author.setDescription("描述:一枚趔趄的猿。现在时间:" + new Date().getTime());
return author;
} @Override
public List<AuthorDto> getAuthorList() {
List<AuthorDto> resultList = new ArrayList<>();
AuthorDto author = new AuthorDto();
author.setBirthday("1990-01-23");
author.setName("姓名:oKong");
author.setSex(Sex.MALE);
author.setHobby(Arrays.asList("电影","旅游"));
author.setDescription("描述:一枚趔趄的猿。现在时间:" + new Date().getTime());
resultList.add(author);
resultList.add(author);
return resultList;
} @Override
public String getAuthorString(String name) {
AuthorDto author = getAuthor(name);
return author.toString();
}
}
@WebService(
targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空间
name = "authorPortType", //portType名称 客户端生成代码时 为接口名称
serviceName = "authorService", //服务name名称
portName = "authorPortName", //port名称
endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定发布webservcie的接口类,此类也需要接入@WebService注解
/**
* 常量类
* @author oKong
*
*/
public class WsConst {
public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice";
}
/**
* cxf配置类
* @author oKong
*
*/
@Configuration
public class CxfWebServiceConfig { //这里需要注意 由于springmvc 的核心类 为DispatcherServlet
//此处若不重命名此bean的话 原本的mvc就被覆盖了。可查看配置类:DispatcherServletAutoConfiguration
//一种方法是修改方法名称 或者指定bean名称
//这里需要注意 若beanName命名不是 cxfServletRegistration 时,会创建两个CXFServlet的。
//具体可查看下自动配置类:Declaration org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
//也可以不设置此bean 直接通过配置项 cxf.path 来修改访问路径的
@Bean("cxfServletRegistration")
public ServletRegistrationBean dispatcherServlet() {
//注册servlet 拦截/ws 开头的请求 不设置 默认为:/services/*
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
} /**
* 申明业务处理类 当然也可以直接 在实现类上标注 @Service
* @author oKong
*/
@Bean
public AuthorService authorService() {
return new AuthorServiceImpl();
} /*
* 非必要项
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
return springBus;
} /*
* 发布endpoint
*/
@Bean
public Endpoint endpoint(AuthorService authorService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), authorService);
endpoint.publish("/author");//发布地址
return endpoint;
}
}

访问:http://127.0.0.1:8080/ws/author?wsdl ,验证是否发布成功。

客户端调用:

        <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>

右键项目文件---webservice--

WsConst.java:

/**
* 常量类
* @author oKong
*
*/
public class WsConst {
public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice";
public static final String SERVICE_ADDRESS= "http://127.0.0.1:8080/ws/author?wsdl"; //为服务端地址
}
/**
* 配置类
*
* @author oKong
*
*/
@Configuration
public class CxfClientConfig { /**
* 以接口代理方式进行调用 AuthorPortType接口
*/
@Bean("cxfProxy")
public AuthorPortType createAuthorPortTypeProxy() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setServiceClass(AuthorPortType.class);
jaxWsProxyFactoryBean.setAddress(WsConst.SERVICE_ADDRESS);//服务地址:http://127.0.0.1:8080/ws/autho return (AuthorPortType) jaxWsProxyFactoryBean.create();
} /*
* 采用动态工厂方式 不需要指定服务接口
*/
@Bean
public Client createDynamicClient() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(WsConst.SERVICE_ADDRESS);
return client;
}
}
/**
* 调用示例
* @author oKong
*
*/
@RestController
@RequestMapping("/cxf")
public class DemoController { @Autowired
Client client; @Autowired
@Qualifier("cxfProxy")
AuthorPortType authorPort; @GetMapping("/getauthorstring")
public String getAuthorString(String authorName) {
return authorPort.getAuthorString(authorName);
} @GetMapping("/getauthor")
public AuthorDto getAuthor(String authorName) {
return authorPort.getAuthorByName(authorName);
} @GetMapping("/getauthorlist")
public List<AuthorDto> getAuthorList() {
return authorPort.getAuthorList();
} @GetMapping("/dynamic/{operation}")
public Object getAuthorStringByDynamic(@PathVariable("operation")String operationName, String authorName) throws Exception {
//这里就简单的判断了
Object[] objects = null;
// client.getEndpoint().getBinding().getBindingInfo().getOperations()
if ("getAuthorList".equalsIgnoreCase(operationName)) {
objects = client.invoke(operationName);
} else if ("getAuthorString".equalsIgnoreCase(operationName)) {
objects = client.invoke(operationName, authorName);
} else if ("getAuthorByName".equalsIgnoreCase(operationName)) {
objects = client.invoke(operationName, authorName);
} else {
throw new RuntimeException("无效的调用方法");
}
return objects != null && objects.length > 0 ? objects[0] : "返回异常";
}
}

端口号配置:

server.port=8090

启动应用,依次访问。查看是否调用成功。

http://127.0.0.1:8090/cxf/getauthorstring?authorName=oKong

//为客户端的地址

参考文献:https://blog.csdn.net/xie19900123/article/details/83986482

idea使用springboot的webservice基于cxf的更多相关文章

  1. springboot整合webservice采用CXF技术

    转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...

  2. 【转】构建基于CXF的WebService服务

    构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...

  3. JAVAEE——BOS物流项目07:WebService入门、apache CXF入门、基于CXF发布CRM服务

    1 学习计划 1.WebService入门 n 什么是WebService n 调用网络上的WebService服务 n SOAP和WSDL概念 n 基于JDK1.7发布一个简单的WebService ...

  4. 开发基于CXF的 RESTful WebService web 项目 webservice发布

    配置步骤 开发基于CXF的 RESTful WebService 1.创建Web项目并导入CXF的jar 2.在Web.xml中配置 CXFServlet <servlet> <se ...

  5. webservice原理及基于cxf开发的基本流程

    一.SOA和webservice SOA(service-Oriented Architecture)是面向服务的架构,是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的 ...

  6. Springboot整合webservice

    Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...

  7. 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

    转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...

  8. 转载 WebService 的CXF框架 WS方式Spring开发

    WebService 的CXF框架 WS方式Spring开发   1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...

  9. Webservice与CXF框架快速入门

    1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...

随机推荐

  1. 洛谷 P4363 [九省联考2018]一双木棋chess 题解

    题目链接:https://www.luogu.org/problemnew/show/P4363 分析: 首先博弈,然后考虑棋盘的规则,因为一个子在落下时它的上面和左面都已经没有空位了,所以棋子的右下 ...

  2. 关于Object.defineProperty 的基础知识

    Object.defineProperty 这个方法大家耳熟能详,可以对 对象的属性进行添加或修改的操作.即可以进行  数据劫持 .vue就是通过这个方法来劫持数据的. 平时我们创建对象的时候,一般通 ...

  3. mysql8.0忘记密码如何操作?

    很不幸,刚安装了MYSQL8,由于密码验证方式的不同,自己折腾了一小会,不小心退出来了,进不去了.从网上面查了一下资料,好多都不是特别好使,最后摸索出来可以进行如下操作: 1. 在配置文件中设置将密码 ...

  4. 关于Servlet小总结

    目录 Servlet Servlet简介 Servlet简单实现 Servlet注解 Servlet生命周期 启动时加载Servlet Servlet客户端 HTTP请求的结构 Servlet服务器 ...

  5. Apache Tomcat 绿色版安装Service(服务)

    1.配置CATALINA_HOME的环境变量:  变量名:CATALINA_HOME  值:tomcat安装或解压的根目录如:c:\Apache tomcat6.0 2.开始->运行->c ...

  6. storm入门demo

    一.storm入门demo的介绍 storm的入门helloworld有2种方式,一种是本地的,另一种是远程. 本地实现: 本地写好demo之后,不用搭建storm集群,下载storm的相关jar包即 ...

  7. idea新建javaweb工程

    最近尝试了idea的使用,将idea建立javaweb工程的步骤记录下来 1.方框里边是重点 2.next后输入工程文件名点击finish 3.如图看到项目文件夹里边没有WEB-INF文件夹及里边的w ...

  8. jquery3和layui冲突导,致使用layui.layer.full弹出全屏iframe窗口时高度152px问题

    项目中使用的jquery版本是jquery-3.2.1,在使用layui弹出全屏iframe窗口时,iframe窗口顶部总是出现一个152px高的滚动窗口无法实现真正全屏,代码如下: <!DOC ...

  9. static import和import的区别

    import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....Cl ...

  10. Jibx 只绑定需要的字段

    栗子:     binding.xml   <?xml version="1.0" encoding="UTF-8"?> <binding&g ...