SpringBoot 整合 WebService

原文:https://blog.csdn.net/pharaohsprince/article/details/75579630
源码:https://github.com/leftso/demo-spring-boot-cxf


SpringBoot整合CXF发布WebService服务和CXF客户端调用。

这里使用SpringBoot2.3.2及Gradle6.4.1构建项目,关键步骤如下:

一、服务端

1. build.gradle

plugins {
    
    
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'top.onefine'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    
    
    compileOnly {
    
    
        extendsFrom annotationProcessor
    }
}

repositories {
    
    
//    mavenCentral()
    maven {
    
     url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    maven {
    
     url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
}

dependencies {
    
    
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // spirngboot升级到2.3之后,hibernate-validator消失,手动引入依赖
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    // https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws
    compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.3.7'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
    
    
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    // https://mvnrepository.com/artifact/junit/junit
    testCompile group: 'junit', name: 'junit', version: '4.13'

}

test {
    
    
    useJUnitPlatform()
}

2. top\onefine\webservice\CommonService.java

服务接口

package top.onefine.webservice;

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

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

}

3. top\onefine\webservice\CommonServiceImp.java

接口实现

package top.onefine.webservice;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

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

	@Override
	public String sayHello(String name) {
    
    

		return "Hello ," + name;
	}

}

4. top\onefine\config\CxfConfig.java

配置cxf服务发布,默认服务在host:port/services/***路径下

package top.onefine.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 top.onefine.webservice.CommonService;

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

	@Autowired
	private CommonService commonService;

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

二、客户端

top\onefine\client\CxfClient.java

这里相当于把Commonservice接口发布在了路径/services/CommonService下,wsdl文档路径为http://localhost:8080/services/CommonService?wsdl,创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)

package top.onefine.client;

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

import top.onefine.webservice.CommonService;

import java.util.Arrays;

public class CxfClient {
    
    

	public static void main(String[] args) {
    
    
		cl1();
		System.out.println("...");
		cl2();
	}

	/**
	 * 方式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 = "one fine";
			// 调用代理接口的方法调用并返回结果
			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;
		try {
    
    
			// invoke("方法名",参数1,参数2,参数3....);
			objects = client.invoke("sayHello", "one fine...");
			System.out.println("返回数据: " + Arrays.toString(objects));
		} catch (java.lang.Exception e) {
    
    
			e.printStackTrace();
		}
	}
}


参考:

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

  2. spirngboot升级到2.3之后,hibernate-validator消失

猜你喜欢

转载自blog.csdn.net/jiduochou963/article/details/107731942