spring boot整合cxf发布webservice服务和cxf客户端调用

spring boot整合cxf发布webservice服务和cxf客户端调用
本案例使用maven方式
核显文件清单
1.pom.xml
<?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.0</modelVersion>

	<groupId>com.leftso</groupId>
	<artifactId>demo-webservice-cxf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo-webservice-cxf</name>
	<description>Demo project for Spring Boot security</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.5.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- CXF webservice -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.1.11</version>
		</dependency>
		<!-- CXF webservice -->

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2.CommonService.java 服务接口
package com.leftso.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * 接口
 * 
 * @author leftso
 *
 */
@WebService(name = "CommonService", // 暴露服务名称
		targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
	@WebMethod
	@WebResult(name = "String", targetNamespace = "")
	public String sayHello(@WebParam(name = "userName") String name);

}

3.接口实现
package com.leftso.webservice;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

/**
 * 接口实现
 * 
 * @author leftso
 *
 */
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
		targetNamespace = "http://webservice.leftso.com/", // 与接口中的命名空间一致,一般是接口的包名倒
		endpointInterface = "com.leftso.webservice.CommonService"// 接口地址
)
@Component
public class CommonServiceImp implements CommonService {

	@Override
	public String sayHello(String name) {

		return "Hello ," + name;
	}

}

4.配置cxf服务发布,默认服务在Host:port/services/***路径下
package com.leftso.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.leftso.webservice.CommonService;

@Configuration
public class CxfConfig {
	@Autowired
	private Bus bus;

	@Autowired
	CommonService commonService;

	/** JAX-WS **/
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(bus, commonService);
		endpoint.publish("/CommonService");
		return endpoint;
	}
}


这里相当于把Commonservice接口发布在了路径/services/CommonService下,wsdl文档路径为
http://localhost:8080/services/CommonService?wsdl

创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)
package com.leftso.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.leftso.webservice.CommonService;

public class CxfClient {
	public static void main(String[] args) {
		cl1();
	}

	/**
	 * 方式1.代理类工厂的方式,需要拿到对方的接口
	 */
	public static void cl1() {
		try {
			// 接口地址
			String address = "http://localhost:8080/services/CommonService?wsdl";
			// 代理工厂
			JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
			// 设置代理地址
			jaxWsProxyFactoryBean.setAddress(address);
			// 设置接口类型
			jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
			// 创建一个代理接口实现
			CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
			// 数据准备
			String userName = "Leftso";
			// 调用代理接口的方法调用并返回结果
			String result = cs.sayHello(userName);
			System.out.println("返回结果:" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 动态调用方式
	 */
	public static void cl2() {
		// 创建动态客户端
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");
		// 需要密码的情况需要加上用户名和密码
		// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
		// PASS_WORD));
		Object[] objects = new Object[0];
		try {
			// invoke("方法名",参数1,参数2,参数3....);
			objects = client.invoke("sayHello", "Leftso");
			System.out.println("返回数据:" + objects[0]);
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
	}
}


调用后返回结果输出为
Hello,Leftso

项目源码GITHUB下载: 点击下载

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
<h3>回答1:</h3><br/>Spring Boot集成CXF框架可以方便地调用Web服务。以下是使用Spring BootCXF调用Web服务的步骤: 1. 在pom.xml文件中添加CXF依赖: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.6</version> </dependency> ``` 2. 创建一个接口来定义Web服务的操作: ``` @WebService public interface MyWebService { @WebMethod String sayHello(String name); } ``` 3. 创建一个实现类来实现接口中定义的操作: ``` @Service @WebService(endpointInterface = "com.example.MyWebService") public class MyWebServiceImpl implements MyWebService { @Override public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 4. 在应用程序的配置文件中添加CXF配置: ``` cxf: servlet: path: /services/* jaxws: properties: javax.xml.ws.soap.http.soapaction.use: "false" ``` 5. 在控制器中注入Web服务调用它: ``` @RestController public class MyController { @Autowired private MyWebService myWebService; @GetMapping("/hello/{name}") public String sayHello(@PathVariable String name) { return myWebService.sayHello(name); } } ``` 6. 启动应用程序并访问Web服务: ``` http://localhost:8080/services/MyWebServiceImpl?wsdl ``` 以上就是使用Spring BootCXF调用Web服务的步骤。 <h3>回答2:</h3><br/>Spring Boot是一个使用习惯优秀的Web应用开发框架,它可以帮助我们快速构建应用,提高开发效率。而CXF是一个开源的WebService框架,它提供了一系列的API和工具来帮助开发人员可以很轻易地实现一个基于SOAP的Web服务。 在Spring Boot调用CXF框架中的WebService,需要进行以下步骤: 1. 添加依赖 在pom.xml中添加CXFSpring Boot Web依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.1</version> </dependency> </dependencies> ``` 2. 编写WebService客户端类 编写一个类来调用CXF产生的webservice服务,其中包括Endpoint指向和需要调用的方法等信息。 ``` @Service public class WebServiceClient { @Autowired private JaxWsProxyFactoryBean jaxWsProxyFactoryBean; private HelloPortType helloPortType; public String sayHello(final String name) { initPortType(); return helloPortType.sayHello(name); } private void initPortType() { if (helloPortType == null) { jaxWsProxyFactoryBean.setServiceClass(HelloPortType.class); jaxWsProxyFactoryBean.setAddress("http://localhost:8080/hello"); helloPortType = (HelloPortType) jaxWsProxyFactoryBean.create(); } } } ``` 3. 编写WebService 通过CXF创建web服务,并实现接口提供服务,返回接口需要的数据。 ``` @javax.jws.WebService(serviceName = "HelloService", portName = "HelloPort", targetNamespace = "http://www.example.org/HelloService/", endpointInterface = "org.example.service.HelloPortType") public class HelloPortTypeImpl implements HelloPortType { private final static Logger LOGGER = LoggerFactory.getLogger(HelloPortTypeImpl.class); @Resource private WebServiceContext webServiceContext; @Override public String sayHello(final String name) { final MessageContext mc = webServiceContext.getMessageContext(); final HttpServletRequest req = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST); LOGGER.info("服务SayHello calling, name: {}, IP addr:{}, sessionId: {}", name, req.getRemoteAddr(), req.getSession().getId()); return String.format("Hello, %s!", name); } } ``` 4. 进行测试 在控制器中注入WebService客户端类,并进行测试。 ``` @RestController @RequestMapping("/test") public class TestController { @Autowired private WebServiceClient webServiceClient; @GetMapping("/sayHello") public String sayHello(@RequestParam(value = "name") final String name) { return webServiceClient.sayHello(name); } } ``` 总的来说,Spring BootCXF框架结合起来使用,可以很方便地调用WebService,提供服务的代码也可以很容易地进行编写。 <h3>回答3:</h3><br/>SpringBoot是一个非常受欢迎的Java框架,其有很多优秀的特性让使用者更方便地进行应用的开发。其中,SpringBootCXF框架结合使用来调用Webservice可以非常简单地完成。本文将介绍SpringBootCXF框架结合使用来调用Webservice。 首先,我们需要在pom.xml文件中加入CXF及相关依赖。 ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.14</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transport-http</artifactId> <version>3.2.14</version> </dependency> ``` 然后,我们需要在配置文件中设置一些参数,具体如下: ```yaml cxf: client: simple.frontend: true logging.enabled: true timeout: connect: 2000 receive: 5000 path: /ws servlet: url-pattern: /soap/* ``` 在上述配置中,我们开启了日志记录,设置了连接超时和读取超时时间,以及指明了Webservice的路径。 接下来就可以创建一个接口来调用我们的Webservice。例如,我们想要调用一个返回学生列表的Webservice: ```java @WebService public interface StudentWebService { @WebMethod List<Student> getStudents(); } ``` 我们使用CXF的JAX-WS Frontend创建了一个接口,同时使用@WebService注解来标记该接口。然后我们就可以创建一个实现类来实现该接口: ```java @Service public class StudentWebServiceImpl implements StudentWebService { @Override public List<Student> getStudents() { // 调用Webservice,返回学生列表 return new ArrayList<>(); } } ``` 在上述实现类中,我们使用@Service注解来标记该类为SpringBoot的一个服务,同时实现了我们在接口中声明的getStudents()方法,去调用Webservice并返回学生列表。在该方法中,我们可以使用Spring提供的RestTemplate或者使用CXF的Client接口来进行调用。 然后我们同样使用CXF开放一个服务端口,供客户端调用: ```java @Configuration public class CxfConfig { @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public ServletRegistrationBean<CXFServlet> cxfServlet() { return new ServletRegistrationBean<CXFServlet>(new CXFServlet(), "/soap/*"); } @Bean public StudentWebService studentWebService() { return new StudentWebServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), studentWebService()); endpoint.publish("/StudentWebService"); return endpoint; } } ``` 在上述代码中,我们创建了一个CXFServlet,并将其映射到/soap/*路径下,同时创建了一个WebService的实现类,在SpringBoot启动时通过Endpoint暴露出来。这样我们就可以在客户端中通过CXF框架来访问Webservice了。 总结一下,SpringBootCXF框架结合使用调用Webservice可以非常方便地完成。我们只需要提供一个接口,实现其方法并使用CXF暴露出去,就可以在客户端通过CXF框架来访问了。同时,我们还可以使用CXF的一些特性来定制化我们的调用过程,包括定制连接超时、读取超时、日志记录等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值