WebService服务端与客户端的测试问题

最近研究了下WebService,于是想写一个demo测试一下,过程如下:

按照网上写的流程编写服务端代码如下:

 1 @WebService
 2 public class WebServiceDemo {
 3     public String sayHello(){
 4         System.out.println("hello");
 5         return "...";
 6     }
 7     public static void main(String[] args){
 8 
 9         Endpoint.publish("http://localhost:8080/two",new WebServiceDemo());
10     }
11 }
View Code

找了个干净的文件夹  打开命令行输入了如下命令

wsimport -s . http://localhost:8080/two?wsdl

生成的文件目录如下:

于是另建了一个项目来测试客户端

在客户端项目里新建两个包,将自动生成的.java文件复制到其中一个包里面。

另一个包写客户端代码如下

1 public class ServiceDemo {
2     public static void main(String[] args){
3         WebServiceDemo w = new WebServiceDemoService().getWebServiceDemoPort();
4         String s = w.sayHello();
5         System.err.println(s+":");
6 
7     }
8 
9 }
View Code

于是以为万事大吉了,结果运行却报以下错误:

Exception in thread "main" com.sun.xml.internal.ws.spi.db.DatabindingException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
两个类具有相同的 XML 类型名称 "{http://chenzht.fraud.byit.com/}sayHello"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
    this problem is related to the following location:
        at com.byit.server.SayHello
        at public javax.xml.bind.JAXBElement com.byit.server.ObjectFactory.createSayHello(com.byit.server.SayHello)
        at com.byit.server.ObjectFactory
    this problem is related to the following location:
        at com.byit.fraud.chenzht.SayHello
两个类具有相同的 XML 类型名称 "{http://chenzht.fraud.byit.com/}sayHelloResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
    this problem is related to the following location:
        at com.byit.server.SayHelloResponse
        at public com.byit.server.SayHelloResponse com.byit.server.ObjectFactory.createSayHelloResponse()
        at com.byit.server.ObjectFactory
    this problem is related to the following location:
        at com.byit.fraud.chenzht.SayHelloResponse

    at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:90)
    at com.sun.xml.internal.ws.spi.db.BindingContextFactory.create(BindingContextFactory.java:167)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:203)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:176)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:95)
    at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:309)
    at com.sun.xml.internal.ws.db.DatabindingImpl.<init>(DatabindingImpl.java:85)
    at com.sun.xml.internal.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:59)
    at com.sun.xml.internal.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:43)
    at com.sun.xml.internal.ws.db.DatabindingFactoryImpl.createRuntime(DatabindingFactoryImpl.java:105)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.buildRuntimeModel(WSServiceDelegate.java:875)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.createSEIPortInfo(WSServiceDelegate.java:892)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.addSEI(WSServiceDelegate.java:855)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:435)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:404)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:386)
    at javax.xml.ws.Service.getPort(Service.java:119)
    at com.byit.server.WebServiceDemoService.getWebServiceDemoPort(WebServiceDemoService.java:72)
    at com.byit.demo.ServiceDemo.main(ServiceDemo.java:8)
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
两个类具有相同的 XML 类型名称 "{http://chenzht.fraud.byit.com/}sayHello"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
    this problem is related to the following location:
        at com.byit.server.SayHello
        at public javax.xml.bind.JAXBElement com.byit.server.ObjectFactory.createSayHello(com.byit.server.SayHello)
        at com.byit.server.ObjectFactory
    this problem is related to the following location:
        at com.byit.fraud.chenzht.SayHello
两个类具有相同的 XML 类型名称 "{http://chenzht.fraud.byit.com/}sayHelloResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
    this problem is related to the following location:
        at com.byit.server.SayHelloResponse
        at public com.byit.server.SayHelloResponse com.byit.server.ObjectFactory.createSayHelloResponse()
        at com.byit.server.ObjectFactory
    this problem is related to the following location:
        at com.byit.fraud.chenzht.SayHelloResponse

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:152)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:96)
    at com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:98)
    at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:79)
    ... 20 more

两个字:郁闷,于是开始找原因,研究了半天,重新指定路径让命令行再次自动生成.java类
wsimport -keep -p omc http://localhost:8080/two?wsdl

 


和之前的作对比,发现了只有这两个不同之处...
问题出在了下面这个地方:


生成的文件中这个接口里面这两个className的值,应该改成你客户端的包路径,就是我们把命令行自动生成的.java 文件复制到客户端的时候,这两个className的值应该改成客户端的包目录,改过来之后再次测试,完美解决以上异常。

输出了:

表示调用成功了!

 

 



 

posted @ 2018-02-08 15:19  小小小涛  阅读(540)  评论(0)    收藏  举报