业务系统与第三方系统交互,获取第三方系统的用户和组织信息,通过WebService调用,第三方系统提供服务,我们编写客户端代码进行调用.传入传出参数base64加密.
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术。
WebService采用HTTP协议传输数据,采用XML格式封装数据(即XML中说明调用远程服务对象的哪个方法,传递的参数是 什么,以及服务对象的 返回结果是什么)。
WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明 HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。SOAP协议 = HTTP协议 + XML数据格式
该WebService的描述语言,说明用哪些方法可以调用.
CXF方式、Xfire方式、AXIS2方式(第三方系统采用这种方式,比较流行)、AXIS1方式
客户端调用代码
WebService地址:http://IP:PORT/spxtjkpt/service/SzftWebService
查看WebService的WSDL.
第一步: 创建WebService的client项目,没有这个选项去下载插件,记住选择Apache Axis版本.
第二步:输入项目名称,自动下载jar包
第三步:输入WebService服务的URL和包路径
第四步:生成代码
PS:如果第三步的框框不小心点没了.
第五步:将代码复制到项目中创建WebService对象进行调用.
#URL为WebService地址
URL portAddress=new URL(Url);
SzftWebService szftWebService =new SzftWebServiceServiceLocator().getSzftWebServicePort(portAddress);
传入参数
传出参数
定义一个对象对象对应传入的request
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Request")
@XmlAccessorType(XmlAccessType.FIELD)
public class OrgVO {
private String PAGENUM;
private String BMDM;
private String JGBS;
public String getPAGENUM() {
return PAGENUM;
}
public void setPAGENUM(String PAGENUM) {
this.PAGENUM = PAGENUM;
}
public String getBMDM() {
return BMDM;
}
public void setBMDM(String BMDM) {
this.BMDM = BMDM;
}
public String getJGBS() {
return JGBS;
}
public void setJGBS(String JGBS) {
this.JGBS = JGBS;
}
}
定义返回对象接收response
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @Date 2018/12/25 11:42
*/
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class OrgResponseXml extends BaseDecode{
private ResultXml resultXml;
private OrgDataXml orgDataXml;
@XmlElement(name = "Result")
public ResultXml getResultXml() {
return resultXml;
}
public void setResultXml(ResultXml resultXml) {
this.resultXml = resultXml;
}
@XmlElement(name = "Data")
public OrgDataXml getOrgDataXml() {
return orgDataXml;
}
public void setOrgDataXml(OrgDataXml orgDataXml) {
this.orgDataXml = orgDataXml;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.List;
/**
* @Date 2018/12/25 11:46
*/
public class OrgDataXml extends BaseDecode{
/**
* 服务器时间
*/
private String serverTime;
/**
* 总页数
*/
private String totalPageNum;
/**
* 当前页数
*/
private String curPageNum;
private List zzjgList; //组织机构集合
@XmlElement(name = "ZZJG")
public List getZzjgList() {
return zzjgList;
}
public void setZzjgList(List zzjgList) {
this.zzjgList = zzjgList;
}
@XmlAttribute(name = "ServerTime")
public String getServerTime() {
return super.getBase64Decode(serverTime);
}
public void setServerTime(String serverTime) {
this.serverTime = serverTime;
}
@XmlAttribute(name = "TotalPageNum")
public String getTotalPageNum() {
return super.getBase64Decode(totalPageNum);
}
public void setTotalPageNum(String totalPageNum) {
this.totalPageNum = totalPageNum;
}
@XmlAttribute(name = "CurPageNum")
public String getCurPageNum() {
return super.getBase64Decode(curPageNum);
}
public void setCurPageNum(String curPageNum) {
this.curPageNum = curPageNum;
}
}
import javax.xml.bind.annotation.XmlElement;
/**
* @Date 2018/12/25 11:54
*/
public class ZZJG {
private String FYDM;
private String BMDM;
private String BMID;
private String MC;
private String FBM;
private String JC;
private String LXR;
private String LXDH;
private String DZ;
private String PCFT;
private String SFJY;
private String PXH;
private String FJM;
private String JGBS;
@XmlElement(name = "FYDM")
public String getFYDM() {
return FYDM;
}
public void setFYDM(String FYDM) {
this.FYDM = FYDM;
}
@XmlElement(name = "BMDM")
public String getBMDM() {
return BMDM;
}
public void setBMDM(String BMDM) {
this.BMDM = BMDM;
}
@XmlElement(name = "BMID")
public String getBMID() {
return BMID;
}
public void setBMID(String BMID) {
this.BMID = BMID;
}
@XmlElement(name = "MC")
public String getMC() {
return MC;
}
public void setMC(String MC) {
this.MC = MC;
}
@XmlElement(name = "FBM")
public String getFBM() {
return FBM;
}
public void setFBM(String FBM) {
this.FBM = FBM;
}
@XmlElement(name = "JC")
public String getJC() {
return JC;
}
public void setJC(String JC) {
this.JC = JC;
}
@XmlElement(name = "LXR")
public String getLXR() {
return LXR;
}
public void setLXR(String LXR) {
this.LXR = LXR;
}
@XmlElement(name = "LXDH")
public String getLXDH() {
return LXDH;
}
public void setLXDH(String LXDH) {
this.LXDH = LXDH;
}
@XmlElement(name = "DZ")
public String getDZ() {
return DZ;
}
public void setDZ(String DZ) {
this.DZ = DZ;
}
@XmlElement(name = "PCFT")
public String getPCFT() {
return PCFT;
}
public void setPCFT(String PCFT) {
this.PCFT = PCFT;
}
@XmlElement(name = "SFJY")
public String getSFJY() {
return SFJY;
}
public void setSFJY(String SFJY) {
this.SFJY = SFJY;
}
@XmlElement(name = "PXH")
public String getPXH() {
return PXH;
}
public void setPXH(String PXH) {
this.PXH = PXH;
}
@XmlElement(name = "FJM")
public String getFJM() {
return FJM;
}
public void setFJM(String FJM) {
this.FJM = FJM;
}
@XmlElement(name = "JGBS")
public String getJGBS() {
return JGBS;
}
public void setJGBS(String JGBS) {
this.JGBS = JGBS;
}
}
import com.sun.jmx.remote.internal.Unmarshal;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import java.io.*;
/**
* @Date 2018年1月29日
*/
public class XmlUtil {
/** 日志 */
private static Logger logger = LoggerFactory.getLogger(XmlUtil.class);
/**
* 对象转xml输出的工具类
* @author pfdu
* @Date 2018年1月29日
* @param obj
* @param filePath
* @throws Exception
*/
public static void bean2Xml(Object obj , String filePath){
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass()); // 获取上下文对象
Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码字符集
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(obj, baos);
OutputStream ops = new FileOutputStream(new File(filePath));
IOUtils.write(baos.toByteArray(), ops);
} catch (Exception e) {
logger.error("Java类转xml时报错:XmlUtil.bean2Xml():"+e.getMessage());
e.printStackTrace();
}
}
public static String bean2Xml(Object obj){
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass()); // 获取上下文对象
Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码字符集
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} catch (Exception e) {
logger.error("Java类转xml时报错:XmlUtil.bean2Xml():"+e.getMessage());
e.printStackTrace();
}
return "";
}
public static T xml2Bean(String xml){
try {
JAXBContext context = JAXBContext.newInstance(); // 获取上下文对象
Unmarshaller unmarshaller = context.createUnmarshaller();
Object b = unmarshaller.unmarshal(new StringReader(xml));
return (T)b;
} catch (Exception e) {
logger.error("Java类转xml时报错:XmlUtil.bean2Xml():"+e.getMessage());
e.printStackTrace();
return null;
}
}
/**
* 根据xml字符串生成对应的bean
* @param clazz
* @param xmlStr
* @return
*/
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
}