whycxzp
2024-10-21 13d2d9bc303a793d47198de8416447be113d7bbc
webService添加结构
1个文件已修改
12个文件已添加
571 ■■■■■ 已修改文件
pom.xml 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/IMS_Service.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/IMS_ServiceImpl.java 108 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/WsClient.java 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/WsConfig.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/request/Api.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/request/RequestParam.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/response/response1/Api.java 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/response/response1/Corporation.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/response/response1/ResponseResult.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/response/response2/ResponseResult2.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/dto/response/response2/UserInfo.java 82 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webService/util/XmlParser.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -258,9 +258,14 @@
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.7</version>
<!--            <version>3.2.7</version>-->
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.3.1</version>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/webService/IMS_Service.java
New file
@@ -0,0 +1,17 @@
package com.whyc.webService;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IMS_Service {
    @WebMethod
    String getKPIValue(String param);
    @WebMethod
    String getStatus(String param);
}
src/main/java/com/whyc/webService/IMS_ServiceImpl.java
New file
@@ -0,0 +1,108 @@
package com.whyc.webService;
import com.whyc.webService.dto.request.Api;
import com.whyc.webService.dto.request.RequestParam;
import com.whyc.webService.dto.response.response1.Corporation;
import com.whyc.webService.dto.response.response1.ResponseResult;
import com.whyc.webService.dto.response.response2.ResponseResult2;
import com.whyc.webService.dto.response.response2.UserInfo;
import com.whyc.webService.util.XmlParser;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@Component
@WebService(targetNamespace = "http://service.webService.whyc.com/",
        endpointInterface = "com.whyc.webService.IMS_Service")
public class IMS_ServiceImpl implements IMS_Service{
    /**
     *
     * @param param xml格式的字符串
     * @return xml格式的字符串
     */
    @Override
    public String getKPIValue(String param) {
        try {
            //解析传入参数
            RequestParam requestParam = XmlParser.parseXmlRequest(param);
            //地域代码,太供提供,作用不明
            String corporationCode = requestParam.getCorporationCode();
            //时间值,作用不明
            String time = requestParam.getTime();
            //指标
            List<Api> apis = requestParam.getApis();
            List<String> apiNameList = apis.stream().map(Api::getName).collect(Collectors.toList());
            if (apiNameList.contains("BusinessSystemLoginRoll")) {
                //单独调用,采用第二个返回值结构
                ResponseResult2 result2 = new ResponseResult2();
                List<UserInfo> userInfoList = new LinkedList<>();
                for (int i = 0; i < 2; i++) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setName("whyc");
                    userInfo.setLdapId("whyc");
                    userInfo.setSubCompany("whyc");
                    userInfo.setCorporation("whyc");
                    userInfo.setDepartment("whyc");
                    userInfo.setBureau("whyc");
                    userInfo.setIsLdapId(1);
                    userInfoList.add(userInfo);
                }
                result2.setUserInfoList(userInfoList);
                return XmlParser.toXml2(result2);
            }
            else if(apiNameList.contains("BusinessSystemOnlineRoll")){
                //单独调用,采用第二个返回值结构
            }
            else {
                //构建返回参数
                ResponseResult result =  new ResponseResult();
                result.setStatus("success");
                result.setMessage("success");
                Corporation corporations = new Corporation();
                corporations.setId(corporationCode);
                result.setCorporation(corporations);
                List<com.whyc.webService.dto.response.response1.Api> apisResponse = new LinkedList<>();
                int size = apis.size();
                for (int i = 0; i < size; i++) {
                    switch (apis.get(i).getName()) {
                        //1注册用户数
                        case "BusinessUserRegNum":
                            com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
                            apiResponse.setName("BusinessUserRegNum");
                            apiResponse.setValue("100");
                            apisResponse.add(apiResponse);
                            break;
                    }
                }
                corporations.setApis(apisResponse);
                return XmlParser.toXml(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 咱不写
     * @param param
     * @return
     */
    @Override
    public String getStatus(String param) {
        return null;
    }
}
src/main/java/com/whyc/webService/WsClient.java
New file
@@ -0,0 +1,39 @@
package com.whyc.webService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import javax.xml.namespace.QName;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class WsClient {
    public static void main(String[] args) throws Exception {
        String url = "http://localhost:8919/fg/ws/IMS_Service?wsdl";
        String param = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" +
                "<info>\n" +
                "\t<CorporationCode>地域代码</CorporationCode>\n" +
                "<!--传入的指标名称可以为一个或多个-->\n" +
                "<Time>时间值</Time>\n" +
                "<api name=\"指标名称1\"></api>\n" +
                "<api name=\"指标名称2\"></api>\t\n" +
                "<api name=\"指标名称n\"></api>\n" +
                "</info>";
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        QName qName = new QName("http://webService.whyc.com/", "getKPIValue");
        TransformerFactory tf = TransformerFactory.newInstance();
        try (InputStream is = new ByteArrayInputStream(param.getBytes("gb2312"))) {
            StreamSource source = new StreamSource(is);
            Object[] value = client.invoke(qName, source);
            System.out.println(value[0]);
        } catch (Exception e) {
            throw new RuntimeException("Convert XML string to Source failed", e);
        }
    }
}
src/main/java/com/whyc/webService/WsConfig.java
New file
@@ -0,0 +1,37 @@
package com.whyc.webService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WsConfig {
    @Autowired
    private IMS_Service service;
    @Autowired
    @Qualifier(Bus.DEFAULT_BUS_ID)
    public Bus bus;
    @Bean
    public ServletRegistrationBean<CXFServlet> servletServletRegistrationBean() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/ws/*");
    }
    //发布服务
    @Bean
    public Endpoint IMS_Service(){
        EndpointImpl endpoint = new EndpointImpl(bus,service);
        endpoint.publish("/IMS_Service");
        return endpoint;
    }
}
src/main/java/com/whyc/webService/dto/request/Api.java
New file
@@ -0,0 +1,27 @@
package com.whyc.webService.dto.request;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@XmlAccessorType(XmlAccessType.FIELD)
public class Api {
    @XmlAttribute(name = "name")
    private String name;
    // Getter and Setter
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Api{" +
                "name='" + name + '\'' +
                '}';
    }
}
src/main/java/com/whyc/webService/dto/request/RequestParam.java
New file
@@ -0,0 +1,54 @@
package com.whyc.webService.dto.request;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "info")
@XmlAccessorType(XmlAccessType.FIELD)
public class RequestParam {
    @XmlElement(name = "CorporationCode")
    private String corporationCode;
    @XmlElement(name = "Time")
    private String time;
    @XmlElement(name = "api")
    private List<Api> apis;
    public String getCorporationCode() {
        return corporationCode;
    }
    public void setCorporationCode(String corporationCode) {
        this.corporationCode = corporationCode;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public List<Api> getApis() {
        return apis;
    }
    public void setApis(List<Api> apis) {
        this.apis = apis;
    }
    @Override
    public String toString() {
        return "RequestParam{" +
                "corporationCode='" + corporationCode + '\'' +
                ", time='" + time + '\'' +
                ", apis=" + apis +
                '}';
    }
}
src/main/java/com/whyc/webService/dto/response/response1/Api.java
New file
@@ -0,0 +1,31 @@
package com.whyc.webService.dto.response.response1;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
public class Api {
    @XmlAttribute(name = "name")
    private String name;
    @XmlElement(name = "value")
    private String value;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}
src/main/java/com/whyc/webService/dto/response/response1/Corporation.java
New file
@@ -0,0 +1,32 @@
package com.whyc.webService.dto.response.response1;
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
public class Corporation {
    @XmlAttribute(name = "id")
    private String id;
    //不需要这个父级标签
    //@XmlElementWrapper(name = "api")
    @XmlElement(name = "api")
    private List<Api> apis;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public List<Api> getApis() {
        return apis;
    }
    public void setApis(List<Api> apis) {
        this.apis = apis;
    }
}
src/main/java/com/whyc/webService/dto/response/response1/ResponseResult.java
New file
@@ -0,0 +1,54 @@
package com.whyc.webService.dto.response.response1;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "return")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseResult {
    @XmlElement(name = "status")
    private String status;
    @XmlElement(name = "message")
    private String message;
    @XmlElement(name = "reason")
    private String reason;
    @XmlElement(name = "Corporation")
    private Corporation corporation;
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
    public Corporation getCorporation() {
        return corporation;
    }
    public void setCorporation(Corporation corporation) {
        this.corporation = corporation;
    }
}
src/main/java/com/whyc/webService/dto/response/response2/ResponseResult2.java
New file
@@ -0,0 +1,27 @@
package com.whyc.webService.dto.response.response2;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/**
 * 10 日登陆人员名单
 * 11 在线人员名单
 * 返回的结果结构
 */
@XmlRootElement(name = "LOGINUSER")
public class ResponseResult2 {
    private List<UserInfo> userInfoList;
    //@XmlElementWrapper(name = "USERINFO") //不需要这个父级
    @XmlElement(name = "USERINFO")
    public List<UserInfo> getUserInfoList() {
        return userInfoList;
    }
    public void setUserInfoList(List<UserInfo> userInfoList) {
        this.userInfoList = userInfoList;
    }
}
src/main/java/com/whyc/webService/dto/response/response2/UserInfo.java
New file
@@ -0,0 +1,82 @@
package com.whyc.webService.dto.response.response2;
import javax.xml.bind.annotation.XmlElement;
public class UserInfo {
    private String ldapId;
    private String corporation;
    private String subCompany;
    private String bureau;
    private String department;
    private String name;
    private int isLdapId;
    // JAXB要求有一个无参数的构造函数
    public UserInfo() {}
    // Getter和Setter方法
    public String getLdapId() {
        return ldapId;
    }
    @XmlElement(name = "LDAPID")
    public void setLdapId(String ldapId) {
        this.ldapId = ldapId;
    }
    public String getCorporation() {
        return corporation;
    }
    @XmlElement(name = "CORPORATION")
    public void setCorporation(String corporation) {
        this.corporation = corporation;
    }
    public String getSubCompany() {
        return subCompany;
    }
    @XmlElement(name = "SUBCOMPANY")
    public void setSubCompany(String subCompany) {
        this.subCompany = subCompany;
    }
    public String getBureau() {
        return bureau;
    }
    @XmlElement(name = "BUREAU")
    public void setBureau(String bureau) {
        this.bureau = bureau;
    }
    public String getDepartment() {
        return department;
    }
    @XmlElement(name = "DEPARTMENT")
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getName() {
        return name;
    }
    @XmlElement(name = "NAME")
    public void setName(String name) {
        this.name = name;
    }
    public int getIsLdapId() {
        return isLdapId;
    }
    @XmlElement(name = "ISLDAPID")
    public void setIsLdapId(int isLdapId) {
        this.isLdapId = isLdapId;
    }
}
src/main/java/com/whyc/webService/util/XmlParser.java
New file
@@ -0,0 +1,54 @@
package com.whyc.webService.util;
import com.whyc.webService.dto.request.RequestParam;
import com.whyc.webService.dto.response.response1.ResponseResult;
import com.whyc.webService.dto.response.response2.ResponseResult2;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
public class XmlParser {
    public static RequestParam parseXmlRequest(String info) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(RequestParam.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(info);
        RequestParam param = (RequestParam) unmarshaller.unmarshal(reader);
        return param;
    }
    public static String toXml(ResponseResult returnInfo) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ResponseResult.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        // 设置是否格式化输出
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        // 设置编码 gb2312
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "gb2312");
        StringWriter writer = new StringWriter();
        marshaller.marshal(returnInfo, writer);
        return writer.toString();
    }
    public static String toXml2(ResponseResult2 returnInfo) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ResponseResult2.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        // 设置是否格式化输出
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        // 设置编码 gb2312
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "gb2312");
        StringWriter writer = new StringWriter();
        marshaller.marshal(returnInfo, writer);
        return writer.toString();
    }
}