axis传xml到webService接口并解析传回的xml

axis传xml到webService接口并解析传回的xml

**//调整格式//
最近开发项目中碰到需要对接webservice接口,以前没怎么接触过,翻百度都没找出可以调通的测试demo,之前使用httpClient去链接,但是没有成功,最终使用axis包的方法测试通过。
我用的项目是gradle项目。
需要的包的地址可以通过这个地址获得:
MavenRepository
需要的包如下:

 implementation group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
// https://mvnrepository.com/artifact/commons-configuration/commons-configuration
    implementation group: 'commons-configuration', name: 'commons-configuration', version: '1.10'
    compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.3.4'
    // https://mvnrepository.com/artifact/com.adobe.flex.framework/rpc
    // https://mvnrepository.com/artifact/com.aliyun/tea-rpc
    implementation group: 'com.aliyun', name: 'tea-rpc', version: '0.1.2'
// https://mvnrepository.com/artifact/wsdl4j/wsdl4j
    implementation group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
// https://mvnrepository.com/artifact/org.apache.axis/axis
    implementation group: 'org.apache.axis', name: 'axis', version: '1.4'
// https://mvnrepository.com/artifact/axis/axis-jaxrpc
    implementation group: 'axis', name: 'axis-jaxrpc', version: '1.4'
    // https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream
    implementation group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.17'
// https://mvnrepository.com/artifact/axis/axis-saaj
    implementation group: 'axis', name: 'axis-saaj', version: '1.4'
    implementation group: 'commons-discovery', name: 'commons-discovery', version: '0.2'

在这里插入图片描述

application.yml的配置配置werService地址和参数:
esb是我建的实体,ENDPOINT:是我的接口地址
NAMESPACE:这是名称空间
METHOD_NAME:这是方法的名称
SERVICE_CODE_PARAM_NAME:参数1
XML_REQ_PARAM_NAME:参数2

这是我的webservice打开后的页面:
这是我的webservice打开后的页面
具体我也没过多去研究,只知道指出的地方为我需要传的参数,我标出来的一个是名称空间一个是方法名称,这几个是我有用到的。

实体的定义:
在这里插入图片描述

这是service接口:
在这里插入图片描述
返回的是我建的返回的实体,用来解析xml的
在这里插入图片描述

import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@XStreamAlias("Response")
public class DocumentRetrievalResponse {
    @XStreamAlias("Header")
    private ResponseHeader header;

    @XStreamAlias("Body")
    private Body body;

    @Data
    public static class Body {
        @XStreamAlias("ResultList")
        private ResultList resultList;
        @XStreamAlias("ResponseCount")
        private ResponseCount responseCount;
        @XStreamAlias("ResultCode")
        private String resultCode;
        @XStreamAlias("ResultContent")
        private String resultContent;
        //这个在xml里面是个对象
        @Data
        public static class ResultList {
        //这个在xml中是个出现多次所以我定义为数组对象
            @XStreamImplicit(itemFieldName="Item")
            private List<Item> item;
            @Data
            public static class Item {

                    @XStreamAlias("BatId")
                    private String batId;

                    @XStreamAlias("RET")
                    private String RET;
                }
            }
        @Data
        public static class ResponseCount {
            @XStreamAlias("FailCount")
            private String failCount;
            @XStreamAlias("SuccCount")
            private String succCount;
        }
        }
}

实现类:
在这里插入图片描述

@Override
    public DocumentRetrievalResponse getReportList(String xml) {
        try {
            String respond = WebServiceUtil.callWebService("", xml);
            System.out.println(respond);
            DocumentRetrievalResponse documentRetrievalResponse = XmlUtil.toBean(respond, DocumentRetrievalResponse.class);
            return documentRetrievalResponse;
        } catch (Exception e) {
            System.out.println("something goes wrong during call webService");
        }
        return null;
    }

主要的调webservice的方法如下:
在这里插入图片描述

import com.limu.boe.service.his.EsbConfig;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;

@org.springframework.stereotype.Service
public class WebServiceUtil {
    @Autowired
    private EsbConfig esbConfig;

    private static EsbConfig staticEsbConfig;

    @PostConstruct
    public void init() {
        staticEsbConfig = esbConfig;
    }

    public static String callWebService(String serviceCode, String xmlReq) throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(staticEsbConfig.getEndpoint());
        call.setOperationName(new QName(staticEsbConfig.getNamespace(), staticEsbConfig.getMethodName()));
        call.addParameter(staticEsbConfig.getServiceCodeParamName(), XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter(staticEsbConfig.getXmlReqParamName(), XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        System.out.println(serviceCode);
        String xmlResp = (String) call.invoke(new Object[] {serviceCode, xmlReq});
        System.out.println(xmlReq);
        System.out.println(xmlResp);
        return xmlResp;
    }
}

这里分别为传入的两个参数,一个是请求号,一个是xml
在这里插入图片描述
解析xml工具类型:
在这里插入图片描述

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlUtil {
    public static String toXml(Object model) {
        XStream xStream = new XStream(new DomDriver());
        xStream.autodetectAnnotations(true);
        return xStream.toXML(model);
    }

    public static <T> T toBean(String xml, Class<T> clazz) {
        XStream xStream = new XStream(new DomDriver());
        XStream.setupDefaultSecurity(xStream);
        xStream.allowTypesByRegExp(new String[] {".*"});
        xStream.processAnnotations(clazz);
        xStream.ignoreUnknownElements();  //忽略未知的元素
        return (T) xStream.fromXML(xml);
    }
}

最后只需要调用service传入参数就可以了。
之前我传入的xml没有用实体去解析,而是直接根据soapui的xml去拼接的xml参数,我这里写了一个方法去调用:
在这里插入图片描述

public DocumentRetrievalResponse https(HrpPutStockMain m,int type) throws IllegalAccessException {
        String xml=getXML(m,type);
        DocumentRetrievalResponse responseXml=requestService.getReportList(xml);
        return responseXml;
    }

到此就结束了,主要碰到依赖没导入全面的时候编译报错,只要依赖没有错的话,调试出来还是很快的。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值