spring-boot整合Cxf的webservice案例

简介: 1.运行环境开发工具:intellij ideaJDK版本:1.8项目管理工具:Maven 4.0.02.Maven Plugin管理 1 2 5 4.0.0 6 7 spring-boot-ws 8 spring-boot-ws 9 1.

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 4.0.0

2.Maven Plugin管理

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>spring-boot-ws</groupId>
 8     <artifactId>spring-boot-ws</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.5.6.RELEASE</version>
15     </parent>
16 
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-web</artifactId>
21         </dependency>
22         <!-- CXF webservice -->
23         <dependency>
24             <groupId>org.apache.cxf</groupId>
25             <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
26             <version>3.1.11</version>
27         </dependency>
28     </dependencies>
29 
30 
31 </project>
View Code

3.WebService方法创建

 1 package com.goku.demo.controller;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 import javax.jws.WebMethod;
 6 import javax.jws.WebParam;
 7 import javax.jws.WebService;
 8 
 9 /**
10  * Created by nbfujx on 2017/11/27.
11  */
12 @Component
13 @WebService(serviceName = "ExampleService"
14         ,targetNamespace="http://controller.demo.goku.com/")
15 public class ExampleController {
16 
17     @WebMethod
18     public String echo(@WebParam(name = "para") String para) {
19     return "hello"+para;
20     }
21 }
View Code

4.CxfConfig配置编写

 1 package com.goku.demo.config;
 2 
 3 import com.goku.demo.controller.ExampleController;
 4 import org.apache.cxf.Bus;
 5 import org.apache.cxf.jaxws.EndpointImpl;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 
10 import javax.xml.ws.Endpoint;
11 
12 /**
13  * Created by nbfujx on 2017/11/27.
14  */
15 @Configuration
16 public class CxfConfig {
17 
18     @Autowired
19     private Bus bus;
20 
21     @Autowired
22     private ExampleController examplecontroller;
23 
24     @Bean
25     public Endpoint endpoint() {
26         EndpointImpl endpoint = new EndpointImpl(bus,examplecontroller);
27         endpoint.publish("/ExampleService");//接口发布在 /NetbarServices 目录下
28         return endpoint;
29     }
30 
31 }
View Code

5.WsApplication启动类编写

 1 package com.goku.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletComponentScan;
 6 
 7 /**
 8  * Created by nbfujx on 2017/10/19.
 9  */
10 // Spring Boot 应用的标识
11 @SpringBootApplication
12 @ServletComponentScan
13 public class WsApplication {
14 
15     public static void main(String[] args) {
16         // 程序启动入口
17         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18         SpringApplication.run(WsApplication.class,args);
19     }
20 }
View Code

6.Cxf客户端编写

 1 package test.com.goku.demo.client;
 2 
 3 
 4 import org.apache.cxf.endpoint.Client;
 5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 6 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 7 
 8 import com.goku.demo.controller.ExampleController;
 9 
10 /**
11  * Created by nbfujx on 2017/11/27.
12  */
13 public class CxfClient {
14 
15     public static void main(String[] args) {
16         echo();
17     }
18 
19     public static void echo() {
20         // 创建动态客户端
21         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
22         Client client = dcf.createClient("http://localhost:8080/services/ExampleService?wsdl");
23         Object[] objects = new Object[0];
24         try {
25             objects = client.invoke("echo", "str");
26             System.out.println("echo:" + objects[0]);
27         } catch (java.lang.Exception e) {
28             e.printStackTrace();
29         }
30     }
31 }
View Code

7.查看测试结果

先启动cxf服务端,再进行客户端调阅

8.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-ws

待续

目录
相关文章
|
9月前
|
人工智能 Cloud Native 安全
DeepSeek + Higress AI 网关/Spring AI Alibaba 案例征集
诚挚地感谢每一位持续关注并使用 Higress 和 Spring AI Alibaba 的朋友,DeepSeek + Higress AI 网关/Spring AI Alibaba 案例征集中。
763 107
|
10月前
|
XML Java 应用服务中间件
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
2453 17
Spring Boot 两种部署到服务器的方式
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
158 0
|
8月前
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
326 0
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
483 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
12月前
|
存储 运维 安全
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
通过以上措施,可以保证Spring Boot项目的配置管理在专业水准上,并且易于维护和管理,符合搜索引擎收录标准。
676 2
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
189 2
|
12月前
|
Java Maven Spring
Spring 小案例体验创建对象的快感
本文介绍了如何在IDEA中创建一个Spring项目,包括项目创建、配置pom.xml文件以引入必要的依赖、编写实体类HelloSpring及其配置文件applicationContext.xml,最后通过测试类TestHelloSpring展示如何使用Spring的bean创建对象并调用方法。
103 0
|
缓存 NoSQL Java
Springboot自定义注解+aop实现redis自动清除缓存功能
通过上述步骤,我们不仅实现了一个高度灵活的缓存管理机制,还保证了代码的整洁与可维护性。自定义注解与AOP的结合,让缓存清除逻辑与业务逻辑分离,便于未来的扩展和修改。这种设计模式非常适合需要频繁更新缓存的应用场景,大大提高了开发效率和系统的响应速度。
435 2
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
106 0
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App