近期项目中需要用到webservice,接口提供方使用.net编写,返回的是自定义对象,对象中有图片。
接口描述文件如下
POST /FJJTMapService/Service.asmx HTTP/1.1
Host: 192.168.5.92
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ChartService xmlns="http://www.fzis.com.cn/">
<type>string</type>
<width>int</width>
<height>int</height>
<imageType>string</imageType>
<fieldnames>string</fieldnames>
<fieldvalues>string</fieldvalues>
<rgbcolors>string</rgbcolors>
<chartType>string</chartType>
<size>string</size>
</ChartService>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ChartServiceResponse xmlns="http://www.fzis.com.cn/">
<ChartServiceResult>
<Xmax>double</Xmax>
<Xmin>double</Xmin>
<Ymax>double</Ymax>
<Ymin>double</Ymin>
<MapByte>base64Binary</MapByte>
<ErrorMessage>string</ErrorMessage>
</ChartServiceResult>
</ChartServiceResponse>
</soap12:Body>
</soap12:Envelope>
wsdl文件如下
//返回对象
<s:complexType name="Map">
- <s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="Xmax" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="Xmin" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="Ymax" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="Ymin" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="MapByte" type="s:base64Binary" />
<s:element minOccurs="0" maxOccurs="1" name="ErrorMessage" type="s:string" />
</s:sequence>
</s:complexType>
//调用方法
- <s:element name="ChartService">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="type" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="width" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="height" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="imageType" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="fieldnames" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="fieldvalues" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="rgbcolors" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="chartType" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="size" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="ChartServiceResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="ChartServiceResult" type="tns:Map" />
</s:sequence>
</s:complexType>
</s:element>
调用过程
try {
// WebService URL
String url = "http://192.168.5.92/FJJTMapService/Service.asmx";
String namespace = "http://www.fzis.com.cn/";
String method = "ChartService";
//创建调用服务
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
call.setSOAPActionURI(namespace+method);
//注册对象
QName qn=new QName(namespace, "Map");
call.registerTypeMapping(RouteThemeResult.class, qn,
new org.apache.axis.encoding.ser.
BeanSerializerFactory(RouteThemeResult.class, qn),
new org.apache.axis.encoding.ser.
BeanDeserializerFactory(RouteThemeResult.class, qn));
// 设置要调用的方法
call.setOperationName(new QName(namespace, method));
// 该方法需要的参数
call.addParameter(new QName(namespace,"type"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"width"),XMLType.XSD_INT,ParameterMode.IN);
call.addParameter(new QName(namespace,"height"),XMLType.XSD_INT,ParameterMode.IN);
call.addParameter(new QName(namespace,"imageType"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"fieldnames"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"fieldvalues"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"rgbcolors"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"chartType"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"size"),XMLType.XSD_STRING,ParameterMode.IN);
// 方法的返回值类型
//call.setReturnType(XMLType.XSD_STRING);
call.setReturnType(qn);
//设置参数
String type = gisChartInfo.getLayername();
int width = gisChartInfo.getWidth();
int height = gisChartInfo.getHeight();
String imageType = gisChartInfo.getImageType();
String fieldnames = gisChartInfo.getFieldnames();
String fieldvalues = gisChartInfo.getFieldvalues();
String rgbcolors = gisChartInfo.getRgbcolors();
String chartType = gisChartInfo.getChartType();
String size = gisChartInfo.getSize();
Object[] parameters = null;
parameters = new Object[]{type,width+"",height+"",imageType,fieldnames,fieldvalues,rgbcolors,chartType,size};
// 调用该方法
byte[] imageBytes ;
RouteThemeResult result = new RouteThemeResult() ;
Object obj = call.invoke(parameters);
if(obj!=null){
System.out.println("ishere:");
if (obj instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)obj;
}
else {
try {
result = (RouteThemeResult) obj;
} catch (java.lang.Exception _exception) {
result = (RouteThemeResult) org.apache.axis.utils.JavaUtils.convert(obj, RouteThemeResult.class);
}
}
}
我在调用接口过程中碰到的难题是对返回对象的接受,返回对象是.net中定义的,在java中接受就需要进行序列化,序列化的问题搞了好几天没进展,最后实在没招了就还是使用啦
AXIS1.4的 wsdl2java 生成客户端,从生成的代码中找到了序列化返回对象的类,修改后直接引用。
返回对象在java中的定义(通过wsdl2java生成)
public class RouteThemeResult implements java.io.Serializable {
private double xmax;
private double xmin;
private double ymax;
private double ymin;
private byte[] mapByte;
private java.lang.String errorMessage;
public RouteThemeResult() {
}
public RouteThemeResult(
double xmax,
double xmin,
double ymax,
double ymin,
byte[] mapByte,
java.lang.String errorMessage) {
this.xmax = xmax;
this.xmin = xmin;
this.ymax = ymax;
this.ymin = ymin;
this.mapByte = mapByte;
this.errorMessage = errorMessage;
}
/**
* Gets the xmax value for this Map.
*
* @return xmax
*/
public double getXmax() {
return xmax;
}
/**
* Sets the xmax value for this Map.
*
* @param xmax
*/
public void setXmax(double xmax) {
this.xmax = xmax;
}
/**
* Gets the xmin value for this Map.
*
* @return xmin
*/
public double getXmin() {
return xmin;
}
/**
* Sets the xmin value for this Map.
*
* @param xmin
*/
public void setXmin(double xmin) {
this.xmin = xmin;
}
/**
* Gets the ymax value for this Map.
*
* @return ymax
*/
public double getYmax() {
return ymax;
}
/**
* Sets the ymax value for this Map.
*
* @param ymax
*/
public void setYmax(double ymax) {
this.ymax = ymax;
}
/**
* Gets the ymin value for this Map.
*
* @return ymin
*/
public double getYmin() {
return ymin;
}
/**
* Sets the ymin value for this Map.
*
* @param ymin
*/
public void setYmin(double ymin) {
this.ymin = ymin;
}
/**
* Gets the mapByte value for this Map.
*
* @return mapByte
*/
public byte[] getMapByte() {
return mapByte;
}
/**
* Sets the mapByte value for this Map.
*
* @param mapByte
*/
public void setMapByte(byte[] mapByte) {
this.mapByte = mapByte;
}
/**
* Gets the errorMessage value for this Map.
*
* @return errorMessage
*/
public java.lang.String getErrorMessage() {
return errorMessage;
}
/**
* Sets the errorMessage value for this Map.
*
* @param errorMessage
*/
public void setErrorMessage(java.lang.String errorMessage) {
this.errorMessage = errorMessage;
}
private java.lang.Object __equalsCalc = null;
public synchronized boolean equals(java.lang.Object obj) {
if (!(obj instanceof RouteThemeResult)) return false;
RouteThemeResult other = (RouteThemeResult) obj;
if (obj == null) return false;
if (this == obj) return true;
if (__equalsCalc != null) {
return (__equalsCalc == obj);
}
__equalsCalc = obj;
boolean _equals;
_equals = true &&
this.xmax == other.getXmax() &&
this.xmin == other.getXmin() &&
this.ymax == other.getYmax() &&
this.ymin == other.getYmin() &&
((this.mapByte==null && other.getMapByte()==null) ||
(this.mapByte!=null &&
java.util.Arrays.equals(this.mapByte, other.getMapByte()))) &&
((this.errorMessage==null && other.getErrorMessage()==null) ||
(this.errorMessage!=null &&
this.errorMessage.equals(other.getErrorMessage())));
__equalsCalc = null;
return _equals;
}
private boolean __hashCodeCalc = false;
public synchronized int hashCode() {
if (__hashCodeCalc) {
return 0;
}
__hashCodeCalc = true;
int _hashCode = 1;
_hashCode += new Double(getXmax()).hashCode();
_hashCode += new Double(getXmin()).hashCode();
_hashCode += new Double(getYmax()).hashCode();
_hashCode += new Double(getYmin()).hashCode();
if (getMapByte() != null) {
for (int i=0;
i<java.lang.reflect.Array.getLength(getMapByte());
i++) {
java.lang.Object obj = java.lang.reflect.Array.get(getMapByte(), i);
if (obj != null &&
!obj.getClass().isArray()) {
_hashCode += obj.hashCode();
}
}
}
if (getErrorMessage() != null) {
_hashCode += getErrorMessage().hashCode();
}
__hashCodeCalc = false;
return _hashCode;
}
// Type metadata
private static org.apache.axis.description.TypeDesc typeDesc =
new org.apache.axis.description.TypeDesc(RouteThemeResult.class, true);
static {
typeDesc.setXmlType(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "Map"));
org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("xmax");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "Xmax"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "double"));
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("xmin");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "Xmin"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "double"));
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("ymax");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "Ymax"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "double"));
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("ymin");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "Ymin"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "double"));
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("mapByte");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "MapByte"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "base64Binary"));
elemField.setMinOccurs(0);
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("errorMessage");
elemField.setXmlName(new javax.xml.namespace.QName("http://www.fzis.com.cn/", "ErrorMessage"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
elemField.setMinOccurs(0);
elemField.setNillable(false);
typeDesc.addFieldDesc(elemField);
}
/**
* Return type metadata object
*/
public static org.apache.axis.description.TypeDesc getTypeDesc() {
return typeDesc;
}
/**
* Get Custom Serializer
*/
public static org.apache.axis.encoding.Serializer getSerializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanSerializer(
_javaType, _xmlType, typeDesc);
}
/**
* Get Custom Deserializer
*/
public static org.apache.axis.encoding.Deserializer getDeserializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanDeserializer(
_javaType, _xmlType, typeDesc);
}
}
当时问什么不直接用wsdl2java生成客户端是因为生成的代码在当时还读不懂,没有可控性
所以选择了自己手写客户端,第一次学习使用webservice,有不足地方请大家指正,共同学习,共同前进。