這裏簡單的講一下如何使用WebService服務端返回對象,先建立一個接口java
@WebService(name = "HelloWorld",targetNamespace = "http://example/")
public interface HelloWorldInterfaces {
@WebMethod
Person getPerson(@WebParam(name = "name")String name,@WebParam(name = "age")int age);
}複製代碼
建立它的實現類,這裏面的main方法就是WebService啓動的入口:
@WebService()
public class HelloWorld implements HelloWorldInterfaces {
@WebMethod
@Override
public Person getPerson(@WebParam(name = "name")String name,@WebParam(name = "age")int age) {
Person person = new Person(name,age);
return person;
}
public static void main(String[] argv) {
String address = "http://127.0.0.1:9000/HelloWorld";
Endpoint.publish(address, new HelloWorld());
}
}複製代碼
建立的返回值對象:api
public class Person {
private String name;
private int age;
public Person(){
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}複製代碼
這裏須要注意,在封裝的時候對象必定要有一個無參的構造方法,不然將會報錯,會產生相似這樣的錯誤:bash
信息: No default constructor found on class model.Person
java.lang.NoSuchMethodException: model.Person.<init>()
at java.lang.Class.getConstructor0(Class.java:2902)
at java.lang.Class.getDeclaredConstructor(Class.java:2066)
at com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:89)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:269)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:658)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:170)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:537)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:516)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:231)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:165)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:355)
at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.unmarshal(BridgeImpl.java:109)
at com.sun.xml.internal.bind.api.Bridge.unmarshal(Bridge.java:222)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.readResponse(ResponseBuilder.java:539)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:110)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
at com.sun.proxy.$Proxy15.getPerson(Unknown Source)
at example.Client.test3(Client.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
java.lang.NoSuchMethodError: model.Person.<init>()複製代碼
,這個問題其實就是WebService建立對象的時候會先去尋找它的無參構造方法,若是咱們一開始就寫了一個有參的構造方法則會報錯。ide
最後在建立一個客戶端,這裏直接使用了單元測試:單元測試
public class Client {
@Test
public void test3() throws Exception{
URL url = new URL("http://127.0.0.1:9000/HelloWorld?wsdl");
QName qName = new QName("http://example/","HelloWorldService");
Service service = Service.create(url,qName);
HelloWorldInterfaces helloWorld = service.getPort(HelloWorldInterfaces.class);
Person person = helloWorld.getPerson("tom",25);
System.out.println(person);
}
}複製代碼