我正在尝试使用 JAX-WS 制作一个简单的项目。我遵循这个例子:https://docs.oracle.com/javaee/7/JEETT.pdf 准确地说是关于 JAX-WS 的头。我已经遵循了所有步骤。不同之处在于我使用 wildfly
作为服务器和 IDE Eclipse,这很重要,因为我必须从命令提示符生成HelloService
。网络服务的代码:
@WebService
public class Hello {
private final String message = "Hello, ";
public Hello() {
}
@WebMethod
public String sayHelloName(String name) {
return message + name + ".";
}
@WebMethod
public String sayHello() {
return message + ".";
}
}
和 servlet 的代码:
@WebServlet(name = "HelloServlet", urlPatterns = { "/HelloServlet" })
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1530081955969170723L;
@WebServiceRef(wsdlLocation = "http://localhost:8080/SampleRest-web/Hello?wsdl")
private HelloService service;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<html lang="en">");
out.println("<head>");
out.println("<title>Servlet HelloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet HelloServlet at " + request.getContextPath() + "</h1>");
out.println("<p>" + sayHello("world") + "</p>");
out.println("</body>");
out.println("</html>");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
processRequest(request, response);
} catch (ServletException e) {
e.printStackTrace();
}
System.out.println("Test2");
}
private String sayHello(java.lang.String arg0) {
try {
Hello port = service.getHelloPort();
return port.sayHelloName(arg0);
}catch (Exception e) {
e.printStackTrace();
}
return "Ne stana";
}
}
带有 ?wsdl 的 url 代码工作正常,它会生成 wsdl 代码。当我调用servlet时,这是我得到的错误:
5:10:37,075 ERROR [stderr] (default task-2) javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
15:10:37,076 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:361)
15:10:37,077 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:350)
15:10:37,078 ERROR [stderr] (default task-2) at javax.xml.ws.Service.getPort(Service.java:99)
15:10:37,078 ERROR [stderr] (default task-2) at soap.endpoints.soap.HelloService.getHelloPort(HelloService.java:72)
15:10:37,078 ERROR [stderr] (default task-2) at webclient.HelloServlet.sayHello(HelloServlet.java:49)
15:10:37,078 ERROR [stderr] (default task-2) at webclient.HelloServlet.processRequest(HelloServlet.java:32)
15:10:37,079 ERROR [stderr] (default task-2) at webclient.HelloServlet.doGet(HelloServlet.java:40)
15:10:37,080 ERROR [stderr] (default task-2) at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
15:10:37,080 ERROR [stderr] (default task-2) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
15:10:37,080 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
15:10:37,081 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
15:10:37,081 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
15:10:37,081 ERROR [stderr] (default task-2) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
15:10:37,081 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:10:37,081 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
15:10:37,081 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
15:10:37,082 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:10:37,082 ERROR [stderr] (default task-2) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
15:10:37,082 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
15:10:37,082 ERROR [stderr] (default task-2) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
15:10:37,082 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:10:37,083 ERROR [stderr] (default task-2) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
15:10:37,083 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,084 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
15:10:37,085 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
15:10:37,086 ERROR [stderr] (default task-2) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
15:10:37,086 ERROR [stderr] (default task-2) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
15:10:37,086 ERROR [stderr] (default task-2) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
15:10:37,086 ERROR [stderr] (default task-2) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
15:10:37,086 ERROR [stderr] (default task-2) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
15:10:37,087 ERROR [stderr] (default task-2) at java.lang.Thread.run(Thread.java:748)
15:10:37,087 ERROR [stderr] (default task-2) Caused by: org.apache.cxf.service.factory.ServiceConstructionException
15:10:37,087 ERROR [stderr] (default task-2) at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:329)
15:10:37,087 ERROR [stderr] (default task-2) at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
15:10:37,087 ERROR [stderr] (default task-2) at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.buildServiceFromWSDL(ReflectionServiceFactoryBean.java:423)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:525)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:261)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:199)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:102)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:91)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:157)
15:10:37,088 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.JaxWsProxyFactoryBean.create(JaxWsProxyFactoryBean.java:142)
15:10:37,089 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.ServiceImpl.createPort(ServiceImpl.java:493)
15:10:37,089 ERROR [stderr] (default task-2) at org.jboss.wsf.stack.cxf.client.ProviderImpl$JBossWSServiceImpl.createPort(ProviderImpl.java:578)
15:10:37,089 ERROR [stderr] (default task-2) at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:359)
15:10:37,089 ERROR [stderr] (default task-2) ... 46 more
15:10:37,089 ERROR [stderr] (default task-2) Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
15:10:37,089 ERROR [stderr] (default task-2) Two classes have the same XML type name "{http://soap/}sayHello". Use @XmlType.name and @XmlType.namespace to assign different names to them.
15:10:37,094 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,094 ERROR [stderr] (default task-2) at soap.endpoints.soap.jaxws_asm.SayHello
15:10:37,094 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,094 ERROR [stderr] (default task-2) at soap.endpoints.soap.SayHello
15:10:37,094 ERROR [stderr] (default task-2) at public javax.xml.bind.JAXBElement soap.endpoints.soap.ObjectFactory.createSayHello(soap.endpoints.soap.SayHello)
15:10:37,094 ERROR [stderr] (default task-2) at soap.endpoints.soap.ObjectFactory
15:10:37,094 ERROR [stderr] (default task-2) Two classes have the same XML type name "{http://soap/}sayHelloResponse". Use @XmlType.name and @XmlType.namespace to assign different names to them.
15:10:37,094 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,094 ERROR [stderr] (default task-2) at soap.endpoints.soap.jaxws_asm.SayHelloResponse
15:10:37,094 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,094 ERROR [stderr] (default task-2) at soap.endpoints.soap.SayHelloResponse
15:10:37,094 ERROR [stderr] (default task-2) at public javax.xml.bind.JAXBElement soap.endpoints.soap.ObjectFactory.createSayHelloResponse(soap.endpoints.soap.SayHelloResponse)
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.ObjectFactory
15:10:37,095 ERROR [stderr] (default task-2) Two classes have the same XML type name "{http://soap/}sayHelloNameResponse". Use @XmlType.name and @XmlType.namespace to assign different names to them.
15:10:37,095 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.jaxws_asm.SayHelloNameResponse
15:10:37,095 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.SayHelloNameResponse
15:10:37,095 ERROR [stderr] (default task-2) at public javax.xml.bind.JAXBElement soap.endpoints.soap.ObjectFactory.createSayHelloNameResponse(soap.endpoints.soap.SayHelloNameResponse)
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.ObjectFactory
15:10:37,095 ERROR [stderr] (default task-2) Two classes have the same XML type name "{http://soap/}sayHelloName". Use @XmlType.name and @XmlType.namespace to assign different names to them.
15:10:37,095 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.jaxws_asm.SayHelloName
15:10:37,095 ERROR [stderr] (default task-2) this problem is related to the following location:
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.SayHelloName
15:10:37,095 ERROR [stderr] (default task-2) at public javax.xml.bind.JAXBElement soap.endpoints.soap.ObjectFactory.createSayHelloName(soap.endpoints.soap.SayHelloName)
15:10:37,095 ERROR [stderr] (default task-2) at soap.endpoints.soap.ObjectFactory
15:10:37,095 ERROR [stderr] (default task-2)
15:10:37,101 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
15:10:37,102 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:460)
15:10:37,102 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:292)
15:10:37,102 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
15:10:37,102 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138)
15:10:37,103 ERROR [stderr] (default task-2) at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
15:10:37,103 ERROR [stderr] (default task-2) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:10:37,103 ERROR [stderr] (default task-2) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
15:10:37,103 ERROR [stderr] (default task-2) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
15:10:37,103 ERROR [stderr] (default task-2) at java.lang.reflect.Method.invoke(Method.java:498)
15:10:37,104 ERROR [stderr] (default task-2) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211)
15:10:37,104 ERROR [stderr] (default task-2) at javax.xml.bind.ContextFinder.find(ContextFinder.java:392)
15:10:37,104 ERROR [stderr] (default task-2) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:618)
15:10:37,104 ERROR [stderr] (default task-2) at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:348)
15:10:37,104 ERROR [stderr] (default task-2) at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:346)
15:10:37,104 ERROR [stderr] (default task-2) at java.security.AccessController.doPrivileged(Native Method)
15:10:37,104 ERROR [stderr] (default task-2) at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:346)
15:10:37,104 ERROR [stderr] (default task-2) at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:247)
15:10:37,104 ERROR [stderr] (default task-2) at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:472)
15:10:37,105 ERROR [stderr] (default task-2) at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:327)
15:10:37,105 ERROR [stderr] (default task-2) ... 58 more
15:10:37,106 INFO [stdout] (default task-2) Test2
我再次设法自己找到了解决方案。问题出在客户端。执行命令时:wsimport
,创建了一个类包,其中它们的
@XmlType(name = "SayHelloName", propOrder = {
"arg0"
})
不行。这个名字是小首字母,导致例外。一旦我改变了它,它就开始工作了。希望这个答案对其他初学者使用 JAX-WS 有所帮助