java使⽤SOAP协议访问webservice接⼝xml转换
最近在对接webservice接⼝使⽤的是⽣成客户端模式,发现并不是很好⽤,尤其是我对接的这个,就⼀个接⼝⼀个类,⼀个类的⼤⼩就2m⽤idea打开,cpu瞬间跑满,卡的不要不要的,所以试试⽤soap协议去访问服务
第⼀步就是将要发送的信息转换为xml格式
JDK中JAXB相关的重要Annotation:(来源于百度百科JAXB)
@XmlType,将Java类或枚举类型映射到XML模式类型
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表⽰JAXB将⾃动绑定Java类中的每个⾮静态的(static)、⾮瞬态的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTY和
XmlAccessType.NONE。
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。
@XmlJavaTypeAdapter,使⽤定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()⽅法),以序列化Java类为XML。
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),⽣成⼀个包装该数组或集合的XML元素(称为包装器)。
@XmlRootElement,将Java类或枚举类型映射到XML元素。
@XmlElement,将Java类的⼀个属性映射到与属性同名的⼀个XML元素。
@XmlAttribute,将Java类的⼀个属性映射到与属性同名的⼀个XML属性。
在以上的注解中,⽤的最多的是@XMLType,@XmlAccessorType,@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Setter@Getter
@XmlRootElement(name ="tns:Get_ScenicSpot_List")
public class GetScenicSpotList {
@XmlElement(name ="ac")
private String ac;
@XmlElement(name ="pw")
private String pw;
@XmlElement(name ="n")
private String n;
@XmlElement(name ="m")
private String m;
}
@Getter
@Setter
@XmlRootElement(name ="soap:Body")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetScenicSpotListBody {
@XmlAttribute(name="soap:encodingStyle")
protected String eapp = PFTConstant.ENCODING_STYLE;
@XmlElement(name ="tns:Get_ScenicSpot_List")
private GetScenicSpotList getScenicSpotList;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name ="", propOrder ={
//        "header",
"body"
})
@XmlRootElement(name ="soap:Envelope")
@Setter
@Getter
public class GetScenicSpotListRequestEntity {
@XmlAttribute(name="xmlns:soap")
protected String soap =Constant.SOAP;
@XmlAttribute(name="xmlns:soapenc")
protected String soapenc = Constant.SOAPENC;
@XmlAttribute(name="xmlns:tns")
protected String tns =Constant.TNS;
@XmlAttribute(name="xmlns:types")
protected String types =Constant.TYPES;
@XmlAttribute(name="xmlns:xsi")
protected String xsi =Constant.XSI;
@XmlAttribute(name="xmlns:xsd")
protected String xsd = Constant.XSD;
@XmlElement(required =true,name="soap:Body")
protected GetScenicSpotListBody body;
}
这⼀段就是上⾯被赋值的那些如:Constant.SOAP 下⾯出现了x代表被替换了,这些⽹址都⽤⾃⼰的就⾏
public static final String SOAP ="/soap/envelope/";
public static final String SOAPENC ="/soap/encoding/";
public static final String TNS ="urn:xxxxx";
public static final String TYPES ="urn:xxxx/encodedTypes";
public static final String XSI ="/2001/XMLSchema-instance";
public static final String XSD ="/2001/XMLSchema";
public static final String ENCODING_STYLE ="/soap/encoding/";
重点来了:实体类转xml⼯具类
@Slf4j
public class JAXBXmlUtil {
/**
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public static String convertToXml(Object obj){
// 创建输出流
StringWriter sw =new StringWriter();
try{
// 利⽤jdk中⾃带的转换类实现
JAXBContext context = Class());            Marshaller marshaller = ateMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,                Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
}catch(JAXBException e){
<("convertToXml get some error==>",e);
}
String();
}
public static String mapToXml(Map<String,Object> map){
// 创建输出流
StringBuilder sb =new StringBuilder();
调用webservice服务sb.append("<xml>\n");
//值
map.forEach((k,v)->{
sb.append("<").append(k).append(">");
sb.String());
sb.append("</").append(k).append(">\n");
});
sb.append("</xml>\n");
String();
}
@SuppressWarnings("unchecked")
/**
* 将String类型的xml转换成对象
*/
public static<T> T convertToObj(String xmlStr,Class<T> clazz){
T xmlObject = null;
try{
JAXBContext context = wInstance(clazz);
// 进⾏将Xml转成对象的核⼼接⼝
Unmarshaller unmarshaller = ateUnmarshaller();
StringReader sr =new StringReader(xmlStr);
xmlObject =(T)unmarshaller.unmarshal(sr);
}catch(JAXBException e){
log.warn("convertToObj get some error==>",e);
}
return xmlObject;
}
}
之后测试⼀下
GetScenicSpotListRequestEntity scenicSpotListRequestEntity =new GetScenicSpotListRequestEntity();
GetScenicSpotListBody scenicSpotListBody =new GetScenicSpotListBody();
GetScenicSpotList scenicSpotList =new GetScenicSpotList();
scenicSpotList.setAc("12312312");
scenicSpotList.setPw("a3523523rggf");
scenicSpotList.setN("100");
scenicSpotList.setM("100");
scenicSpotListBody.setGetScenicSpotList(scenicSpotList);
scenicSpotListRequestEntity.setBody(scenicSpotListBody);
String strXml = vertToXml(scenicSpotListRequestEntity);
最终结构
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Envelope soap="/soap/envelope/"soapenc="/soap/encoding/"tns="urn:x xxxx"types="urn:xxxxx/encodedTypes"xsi="/2001/XMLSchema-instance"xsd="http:/
//2001/XMLSchema" >
<Body encodingStyle="/soap/encoding/">
<Get_ScenicSpot_List>
<ac>12312312</ac>
<pw>a3523523rggf</pw>
<n>100</n>
<m>100</m>
</Get_ScenicSpot_List>
</Body>
</Envelope>
最后就是访问服务啦
HttpHeaders httpHeaders =new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> formEntity =new HttpEntity<>(strXml,httpHeaders);
RestTemplate restTemplate=new RestTemplate();
String result = restTemplate.postForObject("访问的⽹址",formEntity,String.class);
之后就应该能访问到啦
客户端调⽤⽅式
可以使⽤axis2来⽣成客户端代码
调⽤⽅式:与在java中调⽤⽅法⼀样⼀样的,⼀般来说都会以service作为⼊⼝通过实例化service去调⽤其中的⽅法就⾏,要啥给啥
如果缺少pom依赖的话可以去看看

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。