xfire与jsr181 注解的使用
Published by admin on 03月 28, 2010
步骤
1:开发服务类
2.配置xfire中的配置信息
3.web.xml中配置xfire的信息:
4.发布ws信息
5.客户端访问信息
服务端代码的使用如下:
package cn.com.huawei.xfire.jsr.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* 服务端代码使用注解实现
* 此方式类似Net中ws的发布
* 使用WebService注解将一个类注解为ws,name为ws的名称,
* 使用webMethod注解ws的服务的行为(方法)
* 使用WebResult 注解ws服务的结果信息
* 使用webParam注解ws服务的行为的参数信息header=true表示信息是否添加到soap的header中
* @author bailonggang
* 2009-3-9
* 下午10:00:12
*/
@WebService(name = “HelloService”, targetNamespace = “http://www.openuri.org/2004/04/HelloWorld”)
public class HelloServiceImpl {
@WebMethod(operationName=”hello” ,action=”urn:EchoString”)
@WebResult(name=”echoResult”)
public String hello(@WebParam(name=”echoParam” ,header=true)String username)
{
return “Hello ,”+username;
}
}
xfire的配置文件services.xml必须添加到META-INF下面的xfire目录中:
具体原因后文解说:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://xfire.codehaus.org/config/1.0″>
<service>
<name>HelloService</name>
<namespace>cn.csr.com</namespace>
<serviceClass>cn.com.huawei.xfire.jsr.service.HelloServiceImpl</serviceClass>
<serviceFactory>org.codehaus.xfire.annotations.AnnotationServiceFactory</serviceFactory>
</service>
</beans>
注意使用xfire发布ws时必须使用服务接口和服务实现类的发布但是此处没有实现接口却可以发布其实质得益于WebService的注解实现中的endpointInterface()在编译使用使用注解实现接口提供的功能。
web。xml的配置信息如下是:
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” version=”2.5″ xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
上文中使用services.xml关于它的位置:可以参考xfire和ws加载类XFireConfigurableServlet
如下 XFireConfigurableServlet
package org.codehaus.xfire.transport.http;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xbean.spring.context.SpringApplicationContext;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireException;
import org.codehaus.xfire.spring.XFireConfigLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
/**
* XFire Servlet as Dispatcher including a configuration<br>
* of XFire from services.xml in classpath<br>
* <p>
*/
public class XFireConfigurableServlet
extends XFireServlet
{
private static Log log = LogFactory.getLog(XFireConfigurableServlet.class);
//由此处可以直到运行时的默认的配置文件的目录
private final static String CONFIG_FILE = “META-INF/xfire/services.xml”;
private final static String PARAM_CONFIG=”config”;
/**
* Path to configuration file
*/
private String configPath;
/**
* @return
*/
private String getConfigPath()
{
if (configPath == null || configPath.length() == 0)
{
return CONFIG_FILE;
}
return configPath;
}
public XFire createXFire() throws ServletException
{
configPath = getInitParameter(PARAM_CONFIG);
XFire xfire;
try
{
xfire = loadConfig(getConfigPath());
}
catch (XFireException e)
{
throw new ServletException(e);
}
if(xfire == null || xfire.getServiceRegistry() == null || xfire.getServiceRegistry().getServices() == null || xfire.getServiceRegistry().getServices().size() == 0)
{
xfire = super.createXFire();
}
return xfire;
}
public XFire loadConfig(String configPath) throws XFireException
{
XFireConfigLoader loader = new XFireConfigLoader();
loader.setBasedir(getWebappBase());
log.debug(”Loading configuration files relative to ” + loader.getBasedir().getAbsolutePath());
ServletContext servletCtx = getServletContext();
ApplicationContext parent = (ApplicationContext) servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (parent == null)
{
GenericWebApplicationContext webCtx = new GenericWebApplicationContextX();
webCtx.setServletContext(getServletContext());
webCtx.refresh();
parent = webCtx;
}
ApplicationContext newCtx = loader.loadContext(configPath, parent);
if(servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) == null)
{
servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, newCtx);
}
XFire xfire = (XFire) newCtx.getBean(”xfire”);
xfire.setProperty(XFire.XFIRE_HOME, getWebappBase().getAbsolutePath());
return xfire;
}
public void destroy()
{
log.debug(”Destroying XFireConfigurableServlet”);
ServletContext servletCtx = getServletContext();
ApplicationContext appContext = (ApplicationContext)servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (appContext != null &&
appContext instanceof AbstractApplicationContext)
{
// shutdown the beans
((AbstractApplicationContext)appContext).close();
}
super.destroy();
}
public class GenericWebApplicationContextX extends GenericWebApplicationContext
implements SpringApplicationContext
{
public GenericWebApplicationContextX()
{
super();
}
}
}
