发送webService请求BPM流程

webService请求需要以xml的类型发送参数

package com.sany.datacenter.protal.webser.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * @author wxy
 **/
@Data
@EqualsAndHashCode
@XmlRootElement(name = "web:addReview") //方法名
public class RequestAddReview {
    @XmlElement(name = "arg0")//参数设定名称
    private RequestArg arg;

    @XmlTransient
    public RequestArg getArg() {
        return arg;
    }
}

根据接口发布方样例报文规定参数编写bean类
package com.sany.datacenter.protal.webser.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * @author wxy
 **/
@Data
@EqualsAndHashCode
@XmlRootElement(name = "arg0")
public class RequestArg {

    @XmlElement(name = "docCreator")
    private String docCreator;
    @XmlElement(name = "docStatus")
    private String docStatus;

    @XmlElement(name = "docSubject")
    private String docSubject;
    @XmlElement(name = "fdTemplateId")
    private String fdTemplateId;
    @XmlElement(name = "flowParam")
    private String flowParam;
    @XmlElement(name = "formValues")
    private String formValues;
    @XmlElement(name = "fdId")
    private String fdId;

    @XmlElement(name = "attachmentInfo")
    private String attachmentInfo;




    @XmlTransient
    public String getDocCreator() {
        return docCreator;
    }


    @XmlTransient
    public String getAttachmentInfo() {
        return attachmentInfo;
    }
    @XmlTransient
    public String getDocStatus() {
        return docStatus;
    }

    @XmlTransient
    public String getDocSubject() {
        return docSubject;
    }

    @XmlTransient
    public String getFdTemplateId() {
        return fdTemplateId;
    }

    @XmlTransient
    public String getFlowParam() {return flowParam;}

    @XmlTransient
    public String getFormValues() {
        return formValues;
    }

    @XmlTransient
    public String getFdId() {
        return fdId;
    }

}
需要的入参字段名称根据发布规定编写
package com.sany.datacenter.protal.webser.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.xml.bind.annotation.*;

/**
 * @author wxy
 **/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
        "header",
        "body"
})
@XmlRootElement(name = "soapenv:Envelope")
@Data
@EqualsAndHashCode
public class RequestParameters {

    @XmlAttribute(name = "xmlns:soapenv")
    protected String soapEnv = "http://schemas.xmlsoap.org/soap/envelope/";
    @XmlAttribute(name = "xmlns:web")
    protected String web = "http://webservice.review.km.kmss.landray.com/";

    @XmlElement(required = true, name = "soapenv:Header")
    protected RequestHeader header;

    @XmlElement(required = true, name = "soapenv:Body")
    protected RequestBody body;

    @XmlTransient
    public String getSoapEnv() {
        return soapEnv;
    }

    public void setSoapEnv(String soapEnv) {
        this.soapEnv = soapEnv;
    }

    @XmlTransient
    public String getWeb() {
        return web;
    }

    public void setWeb(String web) {
        this.web = web;
    }

    @XmlTransient
    public RequestHeader getHeader() {
        return header;
    }

    public void setHeader(RequestHeader header) {
        this.header = header;
    }

    @XmlTransient
    public RequestBody getBody() {
        return body;
    }

    public void setBody(RequestBody body) {
        this.body = body;
    }
}
发送请求bean类
/**
 * 构建基本参数
 *
 * @param o 动态参数
 * @return RequestParameters
 */
private RequestParameters buildParams(Object o) {
    RequestParameters requestParameters = new RequestParameters();
    RequestHeader requestHeader = new RequestHeader();
    RequestSoapHeader soapHeader = new RequestSoapHeader();
    soapHeader.setUser(bpmConfig.getUser());
    soapHeader.setPassword(bpmConfig.getPassword());
    RequestBody requestBody = new RequestBody();
    if (o instanceof RequestAddReview) {
        requestBody.setAddReview((RequestAddReview) o);
    } else if (o instanceof RequestApproveProcess) {
        requestBody.setApproveProcess((RequestApproveProcess) o);
    } else if (o instanceof RequestApproveReturn) {
        requestBody.setApproveReturn((RequestApproveReturn) o);
    } else if (o instanceof RequestApproveAbandon) {
        requestBody.setApproveAbandon((RequestApproveAbandon) o);
    } else if (o instanceof RequestGetProcessInfo) {
        requestBody.setGetProcessInfo((RequestGetProcessInfo) o);
    }
    requestHeader.setRequestSoapHeader(soapHeader);
    requestParameters.setHeader(requestHeader);
    requestParameters.setBody(requestBody);
    return requestParameters;
}

发入参还有用户名密码都封装到发送请求bean类中
package com.sany.datacenter.protal.util;


import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.namespace.QName;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;


/**
 * @author wxy
 **/
public class JaxbUtils {
    // 多线程安全的Context.
    private final JAXBContext jaxbContext;

    /**
     * @param types 所有需要序列化的Root对象的类型.
     */
    public JaxbUtils(Class<?>... types) {
        try {
            jaxbContext = JAXBContext.newInstance(types);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Java Object->Xml.
     */
    public String toXml(Object root, String encoding) {
        try {
            StringWriter writer = new StringWriter();
            createMarshaller(encoding).marshal(root, writer);
            return writer.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Java Object->Xml, 特别支持对Root Element是Collection的情形.
     */
    public <T> String toXml(Collection<T> root, String rootName, String encoding) {
        try {
            CollectionWrapper wrapper = new CollectionWrapper();
            wrapper.collection = root;

            JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<>(
                    new QName(rootName), CollectionWrapper.class, wrapper);

            StringWriter writer = new StringWriter();
            createMarshaller(encoding).marshal(wrapperElement, writer);

            return writer.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Xml->Java Object.
     */
    @SuppressWarnings("unchecked")
    public <T> T fromXml(String xml) {
        try {
            StringReader reader = new StringReader(xml);
            return (T) createUnmarshaller().unmarshal(reader);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Xml->Java Object, 支持大小写敏感或不敏感.
     */
    @SuppressWarnings("unchecked")
    public <T> T fromXml(String xml, boolean caseSensitive) {
        try {
            String fromXml = xml;
            if (!caseSensitive)
                fromXml = xml.toLowerCase();
            StringReader reader = new StringReader(fromXml);
            return (T) createUnmarshaller().unmarshal(reader);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建Marshaller, 设定encoding(可为Null).
     */
    public Marshaller createMarshaller(String encoding) {
        try {
            Marshaller marshaller = jaxbContext.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            if (StringUtils.isNotBlank(encoding)) {
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
            }
            return marshaller;
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建UnMarshaller.
     */
    public Unmarshaller createUnmarshaller() {
        try {
            return jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 封装Root Element 是 Collection的情况.
     */
    public static class CollectionWrapper {
        @XmlAnyElement
        protected Collection collection;
    }

}
转换xml的工具类
public static String callWebservice(RequestParameters requestParameters, String address) {
    JaxbUtils jaxbUtils = new JaxbUtils(RequestParameters.class, JaxbUtils.CollectionWrapper.class);
    String requestXml = jaxbUtils.toXml(requestParameters, "UTF-8")
            .replaceAll("\\r", "")
            .replaceAll("\\n", "");

    // 发送请求
    //log.info("url: {}, request: {}", address, requestXml);
    String result = HttpRequest.post(address).header("authKey", AppConstants.SANY_IPASS_AUTHKEY).timeout(5000).body(requestXml).execute().body();
    //log.info("response: {}", result);
    return result;
}
发送请求方法
private Map<String, Object> parseXmlStr(String str, String bw) {
    Map<String, Object> body = BeanUtil.beanToMap(XmlUtil.xmlToMap(str).get("soap:Body"));
    return BeanUtil.beanToMap(body.get("ns1:" + bw));
}
结果内容进行转换map
 @Override
 public Map<String, Object> sendBpm(ApplyForPermissionDto applyForPermissionDto, List<TablePageDto> tablePageDtoList, OrgAuditorDTO orgAuditorDTO, String level) {
     log.info("发送表资产申请");
     RequestAddReview addReview = new RequestAddReview();
     RequestArg arg = new RequestArg();
     JSONObject loginName = new JSONObject().fluentPut("LoginName", applyForPermissionDto.getApplyUserAcs());
     arg.setDocCreator(loginName.toJSONString());
     arg.setDocStatus("20");
     arg.setDocSubject("数据表公共流程申请");
     arg.setFdTemplateId(bpmConfig.getBpmTableConfig().getFdTemplateId());
/*     String fileUrl = "fd_attachment;" + applyForPermissionDto.getFile() + ";测试附件";
     arg.setAttachmentInfo(fileUrl);*/
     // arg.setFlowParam("{auditNode:\"请领导审核\"}");
     arg.setFormValues(toBpmParamJsonString(applyForPermissionDto, tablePageDtoList, orgAuditorDTO, level));
     addReview.setArg(arg);

     RequestParameters requestParameters = buildParams(addReview);
     try {
         // 发起请求
         log.info("发起流程入参参数:{}", JSONObject.toJSONString(requestParameters));
         String result = ClientCallUtil.callWebservice(requestParameters, bpmConfig.getAddress());
         return BeanUtil.beanToMap(parseXmlStr(result, "addReviewResponse").get("return"));
     } catch (Exception e) {
         log.error("bpm发布接口解析异常:", e);
         throw new IllegalArgumentException("提交失败");
     }
 }
 发送Bpm接口
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值