调用webservice接口,xml传输

public class XmlConvertUtil {
    /**
     * xml转换成JavaBean
     *
     * @param xml xml格式字符串
     * @param t 待转化的对象
     * @return 转化后的对象
     * @throws Exception JAXBException
     */
    @SuppressWarnings({ "unchecked" })
    public static <T> T convertToJavaBean(String xml, Class<T> t) throws Exception {
        T obj = null;
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = (T) unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    }

    /**
     * JavaBean转换成xml
     * @param obj
     * @param encoding
     * @return
     */
    public static String convertToXml(Object obj, String encoding) {
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding==null?"utf-8":encoding);

            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
public class WebServiceUtil {

    public static String callWeb(String... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        String methodName = "";
        String namespace = "";
        QName qName = new QName(namespace, methodName);
        Client client = dcf.createClient("");
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(qName, params);
        return objects[0].toString();
    }
}

实体类:

@Data
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "Response")
public class XXXResponse {

    @XmlElement(name = "Result")
    @ApiModelProperty(value = "结果")
    private String result;

    @XmlElementWrapper(name = "Results")
    @XmlElement(name = "Result")
    @ApiModelProperty(value = "数据集合")
    private List<Result> results;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值