在用XFire开发webservice时,遇到一个问题,当需要取得对象时,则报如下错误:
Student.java
接口ITestService.java
实现类:ITestServiceImpl.java
业务类:StudentUtility.java
客户端调用类:TestClient.java
[size=large][color=red][b]找了好久,原因Student.java缺少一个无参构造,加上就可以了[/b][/color][/size]
Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Couldn't instantiate class. com.xfire.pojo.Student
org.codehaus.xfire.fault.XFireFault: Couldn't instantiate class. com.xfire.pojo.Student
at org.codehaus.xfire.aegis.type.basic.BeanType.readObject(BeanType.java:196)
at org.codehaus.xfire.aegis.AegisBindingProvider.readParameter(AegisBindingProvider.java:169)
at org.codehaus.xfire.service.binding.AbstractBinding.read(AbstractBinding.java:206)
at org.codehaus.xfire.service.binding.WrappedBinding.readMessage(WrappedBinding.java:51)
at org.codehaus.xfire.soap.handler.SoapBodyHandler.invoke(SoapBodyHandler.java:42)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Client.onReceive(Client.java:406)
at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
at org.codehaus.xfire.client.Client.invoke(Client.java:336)
at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
at $Proxy0.getStudentByName(Unknown Source)
at com.xfire.TestClient.main(TestClient.java:31)
Caused by: java.lang.InstantiationException: com.xfire.pojo.Student
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.codehaus.xfire.aegis.type.basic.BeanType.readObject(BeanType.java:109)
... 17 more
Student.java
package com.xfire.pojo;
public class Student {
private String name;
private int age;
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接口ITestService.java
package com.xfire;
import com.xfire.pojo.Student;
public interface ITestService {
public String example(String message);
public Student getStudentByName(String name);
public int sum(int c1,int c2);
}
实现类:ITestServiceImpl.java
package com.xfire;
import com.xfire.pojo.Student;
//Generated by MyEclipse
public class TestServiceImpl implements ITestService {
private StudentUtility utility;
public TestServiceImpl()
{
utility=new StudentUtility();
}
public String example(String message) {
return message;
}
public Student getStudentByName(String name) {
return utility.getStudentByName(name);
}
public int sum(int c1, int c2) {
return c1+c2;
}
}
业务类:StudentUtility.java
package com.xfire;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xfire.pojo.Student;
public class StudentUtility {
private Map<String, Student> studentmap;
public StudentUtility()
{
studentmap=new HashMap<String, Student>();
studentmap.put("Alvin", new Student("Alvinqq",25));
studentmap.put("ZengQi", new Student("ZengQi",25));
studentmap.put("Shirley", new Student("Shirley",23));
}
public Student getStudentByName(String name)
{
return studentmap.get(name);
}
public List<Student> getStudents()
{
List<Student> list=new ArrayList<Student>();
Set<String> key=new HashSet<String>();
key=studentmap.keySet();
Iterator<String> iter=key.iterator();
while(iter.hasNext())
{
list.add(studentmap.get(iter.next()));
}
return list;
}
}
客户端调用类:TestClient.java
package com.xfire;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.xfire.pojo.Student;
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
Service service=new ObjectServiceFactory().create(ITestService.class);
XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://localhost/webServiceXFire/services/TestService";
try {
ITestService srv=(ITestService) factory.create(service,url);
String result=srv.example("Hello");
System.out.println(result);
System.out.println(srv.sum(2, 6));
System.out.println(srv.getStudentByName("Alvin").getAge()); } catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
[size=large][color=red][b]找了好久,原因Student.java缺少一个无参构造,加上就可以了[/b][/color][/size]