【发布时间】:2012-03-12 14:08:03
【问题描述】:
我对网络服务很生气。
我有一个非常简单的soap webservice:
@Remote
public interface StudentService
{
public String sayHello();
public List<Student> getStudents();
}
还有
@Stateless
@WebService
public class StudentServiceImpl implements StudentService
{
@Override
public String sayHello()
{
return "Hello World";
}
public List<Student> getStudents()
{
List<Student> students = new ArrayList<Student>();
Student st1 = new Student();
st1.setMatricule(1234);
st1.setName("student1");
students.add(st1);
Student st2 = new Student();
st2.setMatricule(5678);
st2.setName("student2");
students.add(st2);
return students;
}
}
和
public class Student implements Serializable
{
private static final long serialVersionUID = 8286393242028201686L;
private int matricule;
private String name;
public int getMatricule()
{
return matricule;
}
public void setMatricule(int matricule)
{
this.matricule = matricule;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
我在 glassfish 3.1 下部署服务。
使用 glassfish 控制台,它正在工作。
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getStudentsResponse xmlns:ns2="http://services.tuto.java.com/">
<return>
<matricule>1234</matricule>
<name>student1</name>
</return>
<return>
<matricule>5678</matricule>
<name>student2</name>
</return>
</ns2:getStudentsResponse>
</S:Body>
</S:Envelope>
使用 php 它也可以工作(对于这两种方法)。
现在有了一个 java 客户端:
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Client
{
public static void main(String[] args) throws Exception
{
String endPoint = "http://localhost:8080/StudentServiceImplService/StudentServiceImpl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName(new QName("http://services.tuto.java.com/","sayHello"));
System.out.println(call.invoke(new Object[0]));
Service service2 = new Service();
Call call2 = (Call) service2.createCall();
call2.setTargetEndpointAddress(new java.net.URL(endPoint));
call2.setOperationName(new QName("http://services.tuto.java.com/","getStudents"));
System.out.println(call2.invoke(new Object[0]));
}
}
第一个电话有效,但第二个电话无效。
Hello World
12-mars-2012 14:53:23 org.apache.axis.client.Call invoke
GRAVE: Exception:
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:345)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at Client.main(Client.java:24)
我能做什么?
在互联网上搜索了很多小时并尝试了不同的解决方案后仍然没有任何效果......
有简单的解决办法吗?
谢谢。
编辑:
也试过了:
public class SoapClient
{
public static void main(String[] args) throws Exception
{
SOAPMappingRegistry smr = new SOAPMappingRegistry();
BeanSerializer beanSer = new BeanSerializer();
smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("http://services.tuto.java.com/", "StudentServiceImplService"),Student.class, beanSer, beanSer);
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setTargetObjectURI("http://services.tuto.java.com/");
call.setMethodName("getStudents");
Response resp;
try
{
resp = call.invoke(new URL("http://8h9l45j:8080/StudentServiceImplService/StudentServiceImpl"), "");
}
catch (SOAPException e)
{
System.err.println("Caught SOAPException (" +
e.getFaultCode() + "): " + e.getMessage());
return;
}
if (!resp.generatedFault())
{
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
if ( value != null )
{
String[] tlist = (String[])value;
System.out.println();
for ( int i = 0; i < tlist.length; i++ )
System.out.println(tlist[i]);
}
}
else
{
Fault fault = resp.getFault();
System.err.println("Generated fault: ");
System.out.println (" Fault Code = "
+ fault.getFaultCode());
System.out.println (" Fault String = "
+ fault.getFaultString());
}
}
有了这个结果:
Caught SOAPException (SOAP-ENV:Client): No Deserializer found to deserialize a ':return' using encoding style 'http://schemas.xmlsoap.org/soap/encoding/'.
【问题讨论】:
-
学生是否实现了可序列化?
-
尝试记录消息。根据错误消息消息错误:遇到子元素,这是不期望的
-
好的,现在我理解了 ':return' 的含义——查看消息的编辑问题——我尝试使用带有矩阵和名称属性、getter 和 setter 的 Return 类。并更改此行: smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("services.tuto.java.com", "return"),Return.class, beanSer, beanSer);但仍然无法正常工作,同样的异常。
-
如何将我的响应肥皂消息中的
更改为 ?
标签: java web-services glassfish-3 axis