第一步就是将要发送的信息转换为xml格式
JDK中JAXB相关的重要Annotation:(来源于百度百科JAXB)
@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 = "http://schemas.xmlsoap.org/soap/envelope/";
public static final String SOAPENC = "http://schemas.xmlsoap.org/soap/encoding/";
public static final String TNS = "urn:xxxxx";
public static final String TYPES = "urn:xxxx/encodedTypes";
public static final String XSI = "http://www.w3.org/2001/XMLSchema-instance";
public static final String XSD = "http://www.w3.org/2001/XMLSchema";
public static final String ENCODING_STYLE ="http://schemas.xmlsoap.org/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 = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
log.error("convertToXml get some error==>",e);
}
return sw.toString();
}
public static String mapToXml(Map<String,Object> map) {
// 创建输出流
StringBuilder sb = new StringBuilder();
sb.append("\n" );
//值
map.forEach((k,v)->{
sb.append("<").append(k).append(">");
sb.append(v.toString());
sb.append("").append(k).append(">\n");
});
sb.append("\n");
return sb.toString();
}
@SuppressWarnings("unchecked")
/**
* 将String类型的xml转换成对象
*/
public static <T> T convertToObj(String xmlStr,Class<T> clazz) {
T xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
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 = JAXBXmlUtil.convertToXml(scenicSpotListRequestEntity);
最终结构
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:xxxxx" xmlns:types="urn:xxxxx/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:Get_ScenicSpot_List>
<ac>12312312ac>
<pw>a3523523rggfpw>
<n>100n>
<m>100m>
tns:Get_ScenicSpot_List>
soap:Body>
soap: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);
之后就应该能访问到啦
解析soap请求访问webservice接口返回的xml值
可以使用axis2来生成客户端代码
调用方式:与在java中调用方法一样一样的,一般来说都会以service作为入口通过实例化service去调用其中的方法就行,要啥给啥
如果缺少pom依赖的话可以去看看
axis2需要pom依赖