Web Service实例(Spring+Hibernate+Web Service)

时间:2024-03-27 22:38:03

一、新建Web Service Project ,如下图所示:Web Service实例(Spring+Hibernate+Web Service)

点击“Finish”后,引入所要用的jar包,本工程所用的jar包如下:

Web Service实例(Spring+Hibernate+Web Service)

其中webservices-api.jarwebservices-extra-api.jarwebservices-extra.jarwebservices-rt.jar通过引入库的方式引入,如下图所示:

Web Service实例(Spring+Hibernate+Web Service)

持久层类ConfigDAOImpl代码如下:

public class ConfigDAOImpl extends HibernateDaoSupport implements

       ConfigDAOInterface {

    @SuppressWarnings("unchecked")

    public ConfigPO findById(String id) {

       getHibernateTemplate().setCacheQueries(true);

    getHibernateTemplate().setQueryCacheRegion("org.bbsws.po.ConfigPO");

       List<ConfigPO> list = getHibernateTemplate().find(

              "from ConfigPO where ID=?", id);

       if (list != null && list.size() > 0) {

           return list.get(0);

       } else {

           return null;

       }

    }

}

 

业务层类ConfigServiceImpl代码如下:

public class ConfigServiceImpl implements ConfigServiceInterface {

    private Log log = LogFactory.getLog(ConfigServiceImpl.class);

    private ConfigDAOInterface configDao = null;

 

    public void setConfigDao(ConfigDAOInterface configDao) {

       this.configDao = configDao;

    }

   

    public ConfigVO findById(String id) throws BBSWSException {

       ConfigVO configVO = new ConfigVO();

       ConfigPO configPO = configDao.findById(id);

       try {

           BeanUtils.copyProperties(configVO, configPO);

       } catch (IllegalAccessException e) {

           log.error("根据id得到论坛配置对象时发生错误,具体内容为:", e);

       } catch (InvocationTargetException e) {

           log.error("根据id得到论坛配置对象时发生错误,具体内容为:", e);

       }

       return configVO;

    }

}

 

创建Web Service所使用的Java Bean如下:

public class ConfigWS {

    private ConfigServiceInterface configServiceInterface = null;

 

    public ConfigWS() {

       configServiceInterface = BeanXMLFactory.getInstance()

              .getConfigService();

    }

   

    public ConfigVO findById(String id) throws BBSWSException {

       return configServiceInterface.findById(id);

    }

}

 

由于JAX-WS不支持Spring注入,所以用BeanXMLFactory来得到Spring中配置的Bean

 

创建Web Service如下图所示:

Web Service实例(Spring+Hibernate+Web Service)

点击“Next”,进行如下配置:

Web Service实例(Spring+Hibernate+Web Service)

点击“Finish”即完成了Web Service的创建。

此时启动Tomcat,输入http://localhost:8080/bbsws/ConfigWSPort即可看配置是否成功。

 

 

二、新建Web Project,此处名为bbs,然后点击“New Web Service Client”,如下图所示:

 Web Service实例(Spring+Hibernate+Web Service)

点击“Next”,如下:

Web Service实例(Spring+Hibernate+Web Service)

一直点击“Next”到如下界面:

Web Service实例(Spring+Hibernate+Web Service)

点击“Finish”即完成了Web Service Client的创建。

 

新建一个测试类如下,

public class Test {

    public static void main(String[] args) throws BBSWSException_Exception {

       ConfigWSService service = new ConfigWSService();

       ConfigWSDelegate delegate = service.getConfigWSPort();

       ConfigVO configVO = delegate.findById("AttachFileType");

       System.out.println(configVO.getID() + ":" + configVO.getConfContext());

    }

}

 

需要将webservices-api.jar拷贝到MyEclipse安装目录下的jre\lib\endorsed中,如果没有endorsed目录则新建下。否则会出现Exception in thread "main" java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/E:/software/Java/workstation/bbs/WebRoot/WEB-INF/lib/webservices-rt.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)异常。