今天用CXF写了个HelloWorld,放在glassfish上一运行,竟然出错!反编译CXF的代码跟踪了半天,原来是抽象类javax.xml.stream.XMLOutputFactory的实现冲突。该类在glassfish/lib/javaee.jar和CXF自带的包geronimo-stax-api_1.0_spec-1.0.1.jar中都有实现。glassfish启动时会先装载javaee.jar包中的class;但CXF需要的是geronimo-stax-api_1.0_spec-1.0.1.jar里面的com.ctc.wstx.stax.WstxOutputFactory实现类。执行时异常信息如下:
Caused by: javax.xml.stream.XMLStreamException: NamespaceURI cannot be null
at com.sun.xml.stream.writers.XMLStreamWriterImpl.writeAttribute(XMLStreamWriterImpl.java:632)
at org.apache.cxf.staxutils.StaxUtils.writeElement(StaxUtils.java:520)
at org.apache.cxf.staxutils.StaxUtils.writeElement(StaxUtils.java:440)
at org.apache.cxf.staxutils.StaxUtils.writeDocument(StaxUtils.java:421)
at org.apache.cxf.staxutils.StaxUtils.writeDocument(StaxUtils.java:411)
at org.apache.cxf.staxutils.StaxUtils.writeNode(StaxUtils.java:560)
at org.apache.cxf.transport.http.WSDLQueryHandler.writeResponse(WSDLQueryHandler.java:226)
解决办法:
在glassfish系统属性中加入javax.xml.stream.XMLOutputFactory=com.ctc.wstx.stax.WstxOutputFactory,这样就让glassfish将WstxOutputFactory作为XMLOutputFactory的实现。
可以通过glassfish的控制台菜单加入,从Configuration --> System Properties进入;或直接修改domain中config目录下的配置文件domain.xml:
<domain ...
<configs>
<config ..
...
<system-property name="javax.xml.stream.XMLOutputFactory" value="com.ctc.wstx.stax.WstxOutputFactory"/>
</config>
</configs>
...
</domain>
修改后需要重启glassfish。这个CXF的HelloWorld在tomcat上是没有问题的,希望对glassfish的新手有点用。
分享到:
相关推荐
HelloWorld client = (HelloWorld) factory.create(); String response = client.sayHello("World"); System.out.println(response); ``` 总结来说,Java调用Web服务主要有JAX-WS和Apache CXF两种方式。JAX-WS是...
HelloWorld proxy = service.getPort(HelloWorld.class); System.out.println(proxy.sayHello("User")); ``` - REST客户端(使用HttpURLConnection): ```java URL url = new URL("http://example.com/hello...
使用Java的部署工具(如Apache CXF或Glassfish服务器),我们可以将这个实现部署为一个Web服务。部署后,它将暴露一个URL,其他系统可以通过这个URL调用`sayHello()`方法。 4. **调用Web服务**: 要调用发布的Web...
HelloWorld helloWorld = service.getHelloWorldPort(); String response = helloWorld.sayHello(); System.out.println(response); ``` 除了基本的SOAP通信,Java EE 5也支持RESTful Web服务,通过JAX-RS(Java ...
此外,还可以考虑使用Apache CXF或Glassfish等其他Web服务框架,它们提供了更丰富的功能和更好的性能。 总结,JDK6为开发WebService提供了基础支持,通过JAX-WS可以方便地创建、发布和消费服务。同时,结合不同的...
return "Hello, World!"; } } ``` 在这个例子中,`@Path("/hello")`注解指定了资源的URI路径,`@GET`和`@Produces(MediaType.TEXT_PLAIN)`分别表示该方法处理GET请求并返回纯文本内容。 然后,我们需要配置...
或者,你可以使用诸如Apache CXF或Glassfish等Web服务容器来部署服务。 4. **生成WSDL**:Java服务端会自动生成一个描述服务的WSDL文件,客户端可以通过这个文件了解如何调用服务。 5. **消费Web Service**:...
【WebService(一):JDK WEB服务API实现】 在IT行业中,WebService是一种...然而,在大型企业级应用中,可能需要考虑使用更强大的框架,如Apache CXF或Glassfish Metro,它们提供了更多高级功能和更好的性能优化。
虽然JDK内置的Web服务支持足够基础使用,但为了更高的性能和灵活性,开发者可能会选择第三方框架,如Apache CXF或Glassfish Metro,这些框架提供了更多高级功能和优化选项。 总之,JDK Webservice涉及到的核心是...