Web Service实例(Spring+Hibernate+Web Servic

系统 1797 0

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

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

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

其中 webservices-api.jar webservices-extra-api.jar webservices-extra.jar webservices-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/ ) 异常。

 

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论