【日常】關於Eclipse開發基於cxf的web services的基本步驟

來源:互聯網
上載者:User

好久之前玩過的東西,昨天應一個哥們要去,給他做一個服務,只好想了又想,因為自己對java一知半解,只停留在照貓畫虎的層次,不過運氣還好,加上以前碰到過很多挫折,所以配的還算順利。在csdn進行記錄,以免忘記。

順序:

 

一 準備

1 Eclipse3.4

2 JDK1.5

3 Tomcat 6

4 tomcat plugin v3.2.1

5 cxf開發包 2.2.5

 

二 配置環境

1 安裝jdk

2 copy eclipse && tomcat6 && cxf 到自己想放它們的地方(eclispe不需要安裝,tomcat從6開始也不需要install)

3 配置環境變數如下: 
  參考

  set CXF_HOME=C:/cxf2.1
  set JAVA_HOME=C:/Java/jdk1.5.0_09

  set PATH=%JAVA_HOME%/bin;%CXF_HOME%/bin;%PATH%
  set CLASSPATH=.;%CXF_HOME%/lib/cxf-manifest.jar;./build/classes

4. 解壓縮tomcat plugin v3.2.1到Eclipse的plugin目錄下

 

三 開發過程

關於開發過程,Apache和IBM官方有很詳細的文檔,

eg:http://www.ibm.com/developerworks/cn/webservices/ws-pojo-springcxf/。

我這裡只針對過程中可能遇到的一個問題進行總結。(之後會附上可行的原始碼)

1 Eclispe下new個新的普通java工程就可以,但是myeclipse下好像只能在建立在web項目中

2 暴露服務有兩種方式jaxws:Endpoint和jaxws:Server兩種方式,網上有很詳細的解釋它們的區分,你可以任選一種

3 有的時候,eclipse會提示找不到Endpoint的說明,沒有問題,go on

4 你要部署一個項目到tomcat, 要寫一個xml到tomcat目錄去:

    參考:

   <?xml version="1.0" encoding="UTF-8"?>
    <Context docBase="E:/自己-CODE/JavaCode/Eclipse34_workspace/你的項目名稱,一定要正確,大小寫匹配"/>

    建議:目錄最好用英文,否則有時候用不同編輯的時候,會出現二進位無法讀取現象,比如上面的“自己”用記事本程式寫入後,

             Eclipse在執行時有時會出錯的,如果你用Eclipse開啟會發現這個記事本開啟能看的“自己”由於編碼問題可能在eclipse中不能

             正常顯示,需要修改。
5 介面和類加@WebService後會暴露介面,你也可以選擇性暴露。

6 附上一個可行的代碼參考:

【1】類介面(函數名字隨便起的,和參數沒有任何關係)

package ws.cxf;

import javax.jws.WebService;

@WebService
public interface IAuthenService
{
 public String  ClientService(String footname,int num);
 }

 

【2】類

 /**
 *
 */
package ws.cxf;

import javax.jws.WebService;

import ws.cxf.IAuthenService;

@WebService
public class AuthenService implements IAuthenService

 private String myfootname="potatoAndTomato";
 private int eatnum=1000;
 
 public  static void main(String[] args)
 {
  System.out.println("Hello,world!Jo 1");
 }

 public String ClientService(String footname, int num)
 {  
  String result="";
  if(this.myfootname.equals(footname))
  {
   result="你不能再吃這個了,buddy!";
  }
  else
  {
   result="你還能吃呀,強!";
   if(num<this.eatnum)
   {
    result+=" 可惜,你要的已經沒有了!";     
   }
   else
   {
    result+="稍等等,一會就給你!";
   }   
  }
  return result;
 }
 
  public String getMyfootname() {
  return myfootname;
 }

 public void setMyfootname(String myfootname) {
  this.myfootname = myfootname;
 }

 public int getEatnum() {
  return eatnum;
 }

 public void setEatnum(int eatnum) {
  this.eatnum = eatnum;
 }

}

 

  【3】配置暴露服務xml

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:jaxws="http://cxf.apache.org/jaxws"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                                http://cxf.apache.org/jaxws
                                http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import Apache CXF Bean Definition -->
           <import resource="classpath:META-INF/cxf/cxf.xml"/>
           <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
           <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<!-- EatService -->
           <bean id="myService2" class="ws.cxf.AuthenService">
               <property name="myfootname" value="qiao"/>
               <property name="eatnum" value="10"/>
           </bean>   
            
          <!-- Expose EatWebService  -->      
           <jaxws:endpoint id="eatWebService2" serviceName="AuthenSrv" address="/AccessControlDomain/Access">
               <jaxws:implementor ref="myService2"/>                  
           </jaxws:endpoint>    

</beans>        

 

【4】項目設定檔xml:放置在WEB-INF下

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <description>在tomcat manager中顯示的描述</description>
 <display-name>在tomccat manager中顯示的名字 </display-name>
 <!-- Spring Config Location -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:/BeanRefServer.xml</param-value>
 </context-param>
 <!-- Spring ContextLoaderListener -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- Apache CXFServlet -->
 <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- CXFServlet Mapping -->
 <servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

 

 

7 運行tomcat:如果提示連接埠被占,可以看看自己電腦上有沒有安裝oracle,它會自動安裝apache服務,會佔用8080連接埠,你需要修改其中一個連接埠,到對應的設定檔中替換8080為一個不常使用的連接埠即可,如8088。

 

8 在瀏覽器下看訪問:127.0.0.1:8080 是會顯示tomcat首頁的,點擊tomcat manager,如果你配置好tomcat的使用者,

在tomcat 6下,使用者配置資訊放在其目錄下的tomcat-user.xml中,預設是注釋掉的,你需要去掉注釋符,然後添加一個角色manager

如下面的使用者tomcat.

<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat,manager"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>

之後,你進入tomcat manager後,就可看見自己部署的項目,點擊即可訪問其。

 

9 oracle的osb可以進行類似於微軟web服務的那種基於瀏覽器的測試,如果你需要可以試試,還是很方便的。

 

以上說明就到此位置,感覺以前遇到了很多小問題,但是一時也想不起來了,先這樣吧。

 

附錄:關於微軟的服務開發和uddi部署,可以參考一個我寫在電腦編程與技巧的文章。

裡面關於UDDI的部署的一些發現,可能在網上很難找到。

http://www.cqvip.com/qk/98258X/201005/33119616.html。

 

說明:我以前看的一個關於cxf開發的很詳細的文章在我的資源中,1分。

 

附錄:

【1】 tomcat manager 查看

 

 

 

【2】 發布介面:

 

That‘s all.

Good luck 2 u.
                                                       

   

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.