[原创于:http://happydev.javaeye.com]
以前写Web Service一直是用Axis来做,用了Seam来开发Web后,这次为了项目的简洁,决定采用“在Seam项目中写JbossWS”的方案来实现WebService。
第一步:实现基于POJO的JBossWS Web Service 写基于POJO的Web Service实现:
@WebService public class TestService { @WebMethod public String test(){ //.... } }
如果在Web Service中要使用到Seam的会话,则需在POJO代码所在位置的META-INF目录中加上standard-jaxws-endpoint-config.xml
<jaxws-config xmlns="urn:jboss:jaxws-config:2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="urn:jboss:jaxws-config:2.0 jaxws-config_2_0.xsd"> <endpoint-config> <config-name>Seam WebService Endpoint</config-name> <pre-handler-chains> <javaee:handler-chain> <javaee:protocol-bindings>##SOAP11_HTTP</javaee:protocol-bindings> <javaee:handler> <javaee:handler-name>SOAP Request Handler</javaee:handler-name> <javaee:handler-class>org.jboss.seam.webservice.SOAPRequestHandler</javaee:handler-class> </javaee:handler> </javaee:handler-chain> </pre-handler-chains> </endpoint-config> </jaxws-config>
在web.xml中注册EndPoint
<!-- Web Service --> <servlet> <servlet-name>TestService Servlet</servlet-name> <servlet-class>test.TestService</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TestService Servlet</servlet-name> <url-pattern>/TestService</url-pattern> </servlet-mapping>
第二步:使得Web Service能够访问Seam组件
如果就上面的代码,在Web Service中是无法访问Seam组件的,会提示没有Seam环境。
在WebService中访问Seam组件:
@WebService public class TestService { @WebMethod public String test(){ TestSeamComp comp = (TestSeamComp) Component.getInstance("testSeamComp"); return comp.test(); } }
@Name("testSeamComp") @AutoCreate public class TestSeamComp { public String test(){ return "from testSeamComp"; } }
如果就使用以上的代码,会抛出Seam环境找不到的异常。
需要在components.xml中做如下配置:
中声明中增加:
xmlns:web="http://jboss.com/products/seam/web"
和
http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd
完整的components.xml头大概如下:
<components xmlns="http://jboss.com/products/seam/components" xmlns:core="http://jboss.com/products/seam/core" xmlns:persistence="http://jboss.com/products/seam/persistence" xmlns:drools="http://jboss.com/products/seam/drools" xmlns:bpm="http://jboss.com/products/seam/bpm" xmlns:security="http://jboss.com/products/seam/security" xmlns:mail="http://jboss.com/products/seam/mail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://jboss.com/products/seam/web" xsi:schemaLocation= "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.0.xsd http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.0.xsd http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd">
再加上:
<web:context-filter url-pattern="/TestService/*"/>
|