SpringBoot_WebService_cxf

Springboot版本对应:

Spring boot 1.4.x ------>cxf-spring-boot-starter-jaxws 3.1.x
Spring boot 1.5.x------->cxf-spring-boot-starter-jaxws 3.2.x
Spring boot 2.x------->cxf-spring-boot-starter-jaxws 3.3.x

服务端

依赖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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.plxc.cxf</groupId>
	<artifactId>springboot-cxf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-cxf</name>
	<description>WebService服务-cxf</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-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--WerbService CXF依赖 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.3.2</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

	</dependencies>

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

</project>

配置源码

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TUser implements Serializable{

	private static final long serialVersionUID = 6258449347837988763L;
	
	private String id;
	private String name;
	private Integer age;
	private String address;
	
}

服务接口

@WebService(name="UserService",targetNamespace="http://service.cxf.plxc.com")
public interface UserService {
	
	@WebMethod
	String getName(@WebParam(name = "id") String id);

	@WebMethod
	TUser getUser(String id);

	@WebMethod
	List<TUser> getAllUser();
}

服务实现类

@WebService(serviceName="UserService",targetNamespace = "http://service.cxf.plxc.com", endpointInterface = "com.plxc.cxf.service.UserService")
public class UserServiceImpl implements UserService {

	@Override
	public String getName(String id) {
		System.err.println("getName...");
		return id;
	}

	@Override
	public TUser getUser(String id) {
		TUser user = new TUser(id, "小明", 28, "上海");
		System.err.println("getUser...");
		return user;
	}

	@Override
	public List<TUser> getAllUser() {
		TUser user1 = new TUser("1", "小明", 28, "上海");
		TUser user2 = new TUser("2", "小明", 28, "上海");
		List<TUser> result = new ArrayList<TUser>();
		result.add(user1);
		result.add(user2);
		System.err.println("getAllUser...");
		return result;
	}

}

配置类

@Configuration
public class WebServiceConfig {
	

	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		return new SpringBus();
	}

	@Bean
	public UserService userService() {
		return new UserServiceImpl();
	}

	/**
	 * 发布服务
	 * 
	 * @return
	 */
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), userService());// 绑定要发布的服务
		endpoint.publish("/user"); // 显示要发布的名称
		return endpoint;
	}
}

客户端

依赖pom.xml

	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxws</artifactId>
		<version>3.3.2</version>
	</dependency>
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http</artifactId>
		<version>3.3.2</version>
	</dependency>

源码

/**
 * 方式一:动态调用
 * @throws Exception
 */
private static void test1() throws Exception {
	JaxWsDynamicClientFactory dcflient = JaxWsDynamicClientFactory.newInstance();
	Client client = dcflient.createClient("http://127.0.0.1:8088/services/user?wsdl");
	Object[] objects = client.invoke("getName", "111");
	System.err.println(objects[0].toString());

	Object[] objectall = client.invoke("getAllUser");
	System.err.println( objectall[0].toString());

}

/**
 * 方式二:通过接口协议获取数据类型
 */
private static void test2() {
	try {
		// 接口地址
		String address = "http://127.0.0.1:8088/services/user?wsdl";
		// 代理工厂
		JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
		// 设置代理地址
		jaxWsProxyFactoryBean.setAddress(address);
		// 设置接口类型
		jaxWsProxyFactoryBean.setServiceClass(UserService.class);
		// 创建一个代理接口实现
		UserService userService = (UserService) jaxWsProxyFactoryBean.create();

		// 设置链接超时和响应时间
		Client proxy = ClientProxy.getClient(userService);
		HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
		HTTPClientPolicy policy = new HTTPClientPolicy();
		policy.setConnectionTimeout(1000);
		policy.setReceiveTimeout(1000);
		conduit.setClient(policy);

		System.err.println(userService.getName("test"));
		System.err.println(userService.getAllUser().size());
		System.err.println(userService.getUser("test"));
	} catch (Exception e) {
		e.printStackTrace();
	}

}

/**
 * 
 * 方式三:部分依赖生成代码(需要通过wsimport -s . -d . -p com.example.demo.stub http://localhost:8088/services/user?wsdl生成类)
 * 
 * @throws Exception
 */
private static void test3() throws Exception {
	// wsdl网络路径
	URL url = new URL("http://127.0.0.1:8088/services/user?wsdl");
	// 服务描述中服务端点的限定名称 两个参数分别为 命名空间 服务名
	QName qName = new QName("http://service.cxf.plxc.com", "UserService");
	Service service = Service.create(url, qName);
	UserService userService = service.getPort(UserService.class);
	System.err.println(userService.getName("test"));
	System.err.println(userService.getAllUser().size());
	System.err.println(userService.getUser("test"));
}

/**
 * 
 * 方式四:完全依赖生成代码(需要通过wsimport -s . -d . -p com.example.demo.stub http://localhost:8088/services/user?wsdl生成类)
 * 
 */
private static void test4() {
	UserService_Service userService_Service = new UserService_Service();
	UserService userService = userService_Service.getUserServiceImplPort();
	System.err.println(userService.getName("test"));
	System.err.println(userService.getAllUser().size());
	System.err.println(userService.getUser("test"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值