ludc
2023-05-17 ed5768da1363742eee34c6c71db010099da6fad8
以及发布webservice接口功能搭建,将通通用接口移植过来
已修改5个文件
336 ■■■■■ 文件已修改
Source/UBCS/ubcs-service/ubcs-webservice/pom.xml 113 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/WsClientUtil.java 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/annotation/VciWebservice.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/config/AttributeMapConfig.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/config/VciCxfPublishConfig.java 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-webservice/pom.xml
@@ -0,0 +1,113 @@
<?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">
    <parent>
        <groupId>com.vci.ubcs</groupId>
        <artifactId>ubcs-service</artifactId>
        <version>3.0.1.RELEASE</version>
    </parent>
    <artifactId>ubcs-webservice</artifactId>
    <name>${project.artifactId}</name>
    <version>${bladex.project.version}</version>
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jul</artifactId>
            <version>2.17.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.vci.ubcs</groupId>
            <artifactId>ubcs-util-api</artifactId>
            <version>3.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <configuration>
                    <username>${docker.username}</username>
                    <password>${docker.password}</password>
                    <repository>${docker.registry.url}/${docker.namespace}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/WsClientUtil.java
@@ -0,0 +1,61 @@
/*
package com.vci.ubcs.webservice;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.commons.lang3.StringUtils;
public class WsClientUtil {
    */
/**
     * axis2调用客户端
     *
     * @param url                   请求服务地址
     * @param nameSpace             命名空间
     * @param soapAction soapAction
     * @param method                方法名
     * @param paramName                参数名称
     * @param sendData                请求报文
     * @param timeOutInMilliSeconds 超时时间
     * @return String 类型的响应报文
     *//*
    public static String sendMsg(String url, String nameSpace,String soapAction, String method, String paramName, String sendData, long timeOutInMilliSeconds) throws Exception {
        ServiceClient serviceClient = new ServiceClient();
        Options option = new Options();
        option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
        option.setTransportInProtocol(Constants.TRANSPORT_HTTP);
        // 值为targetNamespace+methodName
        if(StringUtils.isEmpty(soapAction)){
            soapAction = nameSpace+method;
        }
        option.setAction(soapAction);
        EndpointReference epfs = new EndpointReference(url);
        option.setTo(epfs);
        serviceClient.setOptions(option);
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace namespaceOM = fac.createOMNamespace(nameSpace, "");
        OMElement element = fac.createOMElement(method, namespaceOM);
        OMElement theCityCode = fac.createOMElement(paramName, namespaceOM);
        theCityCode.setText(sendData);
        element.addChild(theCityCode);
        OMElement resultOM = serviceClient.sendReceive(element);
        String result = resultOM.getFirstElement().getText();
        return result;
    }
}
*/
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/annotation/VciWebservice.java
@@ -0,0 +1,36 @@
package com.vci.ubcs.webservice.annotation;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * webservice的实现类
 * @author weidy
 * @date 2022-08-27
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface VciWebservice {
    /**
     * bean的名称
     * @return
     */
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
    /**
     * 路径,默认等于当前bean的名字
     * @return 路径
     */
    String path() default "";
}
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/config/AttributeMapConfig.java
@@ -0,0 +1,41 @@
package com.vci.ubcs.webservice.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@ConfigurationProperties(prefix="attrconfig")
@Component
public class AttributeMapConfig {
    private Map<String,String> system_attrmap;
    /****
     * 无人机主数据与ERP集成字段值转换
     */
    private Map<String,String> attrKey1ToKey2Map;
    public Map<String, String> getSystem_attrmap() {
        return system_attrmap;
    }
    public void setSystem_attrmap(Map<String, String> system_attrmap) {
        this.system_attrmap = system_attrmap;
    }
    public Map<String, String> getAttrKey1ToKey2Map() {
        return attrKey1ToKey2Map;
    }
    public void setAttrKey1ToKey2Map(Map<String, String> attrKey1ToKey2Map) {
        this.attrKey1ToKey2Map = attrKey1ToKey2Map;
    }
    @Override
    public String toString() {
        return "AttributeMapConfig{" +
            "system_attrmap=" + system_attrmap +
            ", attrKey1ToKey2Map=" + attrKey1ToKey2Map +
            '}';
    }
}
Source/UBCS/ubcs-service/ubcs-webservice/src/main/java/com/vci/ubcs/webservice/config/VciCxfPublishConfig.java
@@ -0,0 +1,85 @@
package com.vci.ubcs.webservice.config;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.vci.ubcs.starter.web.util.ApplicationContextProvider;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import com.vci.ubcs.webservice.annotation.VciWebservice;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import java.util.Map;
/**
 * 发布服务类
 * @author weidy
 * @date 2020/3/27
 */
@Configuration
@Slf4j
public class VciCxfPublishConfig {
    /**
     * 注入cxf的bus
     */
    @Autowired(required = false)
    private Bus bus;
    /**
     * 发布站点服务
     * @return 站点服务
     */
    @Bean
    public void autoPushCxf(){
        log.info("开始进行自动发布webService接口");
        Map<String, Object> beansWithAnnotation =   ApplicationContextProvider.getApplicationContext().getBeansWithAnnotation(VciWebservice.class);
        if(!CollectionUtils.isEmpty(beansWithAnnotation)){
            //找这些bean的
            beansWithAnnotation.forEach((beanName,bean)->{
                Class<?> targetClass = AopUtils.getTargetClass(bean);
                VciWebservice annotation = targetClass.getDeclaredAnnotation(VciWebservice.class);
                if(annotation == null){
                    targetClass.getAnnotation(VciWebservice.class);
                }
                if(annotation!=null){
                    //我们去找路径和名称
                    String name = annotation.value();
                    if(StringUtils.isBlank(name)){
                        name = VciBaseUtil.toLowForFirst(targetClass.getSimpleName()).replace(".class","");
                    }
                    String path = annotation.path();
                    if(StringUtils.isBlank(path)){
                        path = "/" + name;
                    }
                    //注册
                    EndpointImpl endpoint = new EndpointImpl(bus,bean);
                    endpoint.publish(path);
                    log.info(String.format("发布接口地址:[%s]", path));
                    registerBean(endpoint,name + "_EndPoint");
                }
            });
        }
        log.info("weBservice接口自动发布结束");
    }
    /**
     *  动态注入bean
     * @param singletonObject 注入对象
     * @param beanName bean名称
     */
    public  void registerBean(Object singletonObject,String beanName) {
        //获取BeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory();
        //动态注册bean.
        defaultListableBeanFactory.registerSingleton(beanName, singletonObject);
    }
}