代码开发需求的过程中会有一些对接第三方或者提供能力给第三方的情况,本连续剧将一步步讲解如何搭建能力开放平台,本项目需求为提供webservice接口给第三方,故采用webservice的集成开始。
代码环境:
IDE:

JDK:

SpringBoot:

maven:

话不多说 , 直接上代码
1:创建基本的springboot项目 pom增加
cxf-spring-boot-starter-jaxws依赖 <?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 https://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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.inspur.open.platform</groupId>
<artifactId>open-platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>open-platform</name>
<description>Demo project for open platform</description>
<properties>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<!-- jaxws webservice依赖包 开始-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.4</version>
</dependency>
<!-- jaxws webservice依赖包 结束-->
<!--启动springboot 会开启部分校验-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2:代码目录结构

3:写一个简单的接口类
package com.inspur.open.platform.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @author wuchengxue
*/
@WebService(name = "UserService", targetNamespace = "http://service.platform.open.inspur.com")
public interface UserService {
/**
* 用户webservice接口
*
* @param name
* @return
*/
@WebMethod
public Object getUserName(@WebParam(name = "name") String name);
}
4:接口实现
package com.inspur.open.platform.service.impl;
import com.inspur.open.platform.base.BaseService;
import com.inspur.open.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
/**
* @author wuchengxue
*/
@Service
@WebService(name = "UserService",
targetNamespace = "http://service.platform.open.inspur.com",
endpointInterface = "com.inspur.open.platform.service.UserService")
public class UserServiceImpl implements UserService {
@Override
public Object getUserName(String name) {
//此处调用各项目模块的微服务逻辑 不进行具体的业务逻辑处理
System.out.println("UserService接口实现执行。。。。。。。"+name);
return name;
}
}
5:接口暴漏配置
package com.inspur.open.platform.config;
import com.inspur.open.platform.constant.WebServiceConstant;
import com.inspur.open.platform.service.UserService;
import com.inspur.open.platform.util.CxfUtil;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* webservice接口暴漏配置类
* 此处控制接口的发布 关闭
*
* @author wuchengxue
*/
@Configuration
public class CxfConfig {
@Autowired
private UserService userService;
/**
* 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
* 统一的webservice接口URL路径
*
* @return
*/
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 注册发布userservice接口到webservice服务
*
* @return
*/
@Bean(name = "UserServiceEndpoint")
public Endpoint userServiceEndpoint() {
//如果没注册 不进行发布
if (CxfUtil.validatePublish(WebServiceConstant.USER_SERVICE)) {
return null;
}
EndpointImpl userEndpoint = new EndpointImpl(springBus(), userService);
userEndpoint.publish(WebServiceConstant.USER_SERVICE);
return userEndpoint;
}
}6:常量类
package com.inspur.open.platform.constant;
import java.util.HashMap;
import java.util.Map;
/**
* @author wuchengxue
*/
public class WebServiceConstant {
public static final String USER_SERVICE="userService";
public static final Map<String, String> SERVICE = new HashMap<>();
static {
//此处控制webservice接口是否publish 暂时用map存储 原则上存储数据库,读取缓存
SERVICE.put("userService","userService");
}
}
7:工具类
package com.inspur.open.platform.util;
import com.inspur.open.platform.constant.WebServiceConstant;
import org.apache.cxf.common.util.StringUtils;
/**
* @author wuchengxue
*/
public class CxfUtil {
/**
* 校验接口是否发布
* @param service
* @return
*/
public static Boolean validatePublish(String service) {
//没注册了返回
if (StringUtils.isEmpty(WebServiceConstant.SERVICE.get(service))) {
return true;
}
return false;
}
}
8:项目启动

9:启动成功,webservice接口发布完成
10:测试类启动
package com.inspur.open.platform.test;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
* @author wuchengxue
*/
public class OpenPlatFormTest {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
testUserService(dcf);
}
/**
* 测试userService
* @param dcf
*/
public static void testUserService(JaxWsDynamicClientFactory dcf){
Client client = dcf.createClient("http://localhost:8081/webservice/userService?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getUserName", "testUserService..");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}11:整合webservice接口发布完成
版权声明:本文为weixin_39543881原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。