4

我已经实现了 Web 服务 MyWebServiceImpl ,例如:

@WebServiceClient(//my parameters//)
@HandlerChain(file = "handlers.xml")
public class MyWebServiceImpl {...}

我也有接口 MyWebService 像

@WebService(//my parameters//)
@XmlSeeAlso({
com.my.ObjectFactory.class,
com.my.second.ObjectFactory.class,
com.my.third.ObjectFactory.class
})
public interface MyWebService {...}

我想使用 Soap 处理程序获得响应,所以我创建了 handlers.xml、LoggingHandler。然后我尝试执行我的测试类我得到错误:

javax.xml.ws.WebServiceException: {http://service.ws.my.com/}MyWebServiceImpl is not a valid service. Valid services are: {http://service.ws.my.com/}myWebService
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:223)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:168)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:96)
at javax.xml.ws.Service.<init>(Service.java:77)
at com.my.ws.service.MyWebServiceImpl.<init>(MyWebServiceImpl.java:46)
at test.MainTest.<init>(MainTest.java:354)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

1 回答 1

14

看起来您的serviceName参数与WSDL 中指定的服务名称不匹配。查看您的 WSDL 文件,您应该会找到以下片段:super(...)MyWebServiceImpl

<wsdl:service name="myWebService">

将 serviceName 参数替换为正确的参数(即myWebService)。

我建议使用wsimport工具而不是手动实现整个客户端存根。它可以从命令行使用,作为一个 ant 任务和一个 maven 插件。您只需要指定 WSDL 文件或 URL。这样您就可以避免繁琐的工作。有很多文档,例如标准 Oracle 手册

当然生成的@WebServiceClient 不会有@HandlerChain 注释。但是可以在运行时轻松添加其他处理程序,例如:

MyWebServiceImplService service = new MyWebServiceImplService();
MyWebService myWebService = service.getMyWebServiceImplPort();
List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.add(new LoggingHandler());
((BindingProvider) myWebService).getBinding().setHandlerChain(
        handlerChain);

System.out.println(myWebService.hello("world"));
于 2013-10-18T20:47:32.090 回答