一、新建
Web Service Project
,如下圖所示:
點擊“ Finish ”后,引入所要用的 jar 包,本工程所用的 jar 包如下:
其中 webservices-api.jar 、 webservices-extra-api.jar 、 webservices-extra.jar 和 webservices-rt.jar 通過引入庫的方式引入,如下圖所示:
持久層類 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 如下圖所示:
點擊“ Next ”,進行如下配置:
點擊“ Finish ”即完成了 Web Service 的創建。
此時啟動 Tomcat ,輸入 http://localhost:8080/bbsws/ConfigWSPort 即可看配置是否成功。
?
?
二、新建 Web Project ,此處名為 bbs ,然后點擊“ New Web Service Client ”,如下圖所示:
點擊“ Next ”,如下:
一直點擊“ Next ”到如下界面:
點擊“ 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/ ) 異常。
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
