ice world

          There is nothing too difficult if you put your heart into it.
          posts - 104, comments - 103, trackbacks - 0, articles - 0

          多系統(tǒng)(異構(gòu)系統(tǒng))進(jìn)行交互時,一種良好的方式便是調(diào)用Web Service,本示例基于Apache組織的CXF,為了方便起見特將服務(wù)端和客戶端寫在同一個工程下,實際項目中是不可能的,但是客戶端卻依賴于服務(wù)端的Web Service接口,那么可以通過導(dǎo)出jar的方式。

          環(huán)境:
          MyEclipse10
          JDK6
          Tomcat7
          CXF2.5
          Spring3

          示例項目結(jié)構(gòu)圖:


          如上圖所示,全部依賴的第三方庫都在lib中,下面貼出全部代碼。
          IHelloService.java
          package bing.server;

          import javax.jws.WebService;

          /**
          * <p>
          * WebService接口
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          @WebService
          public interface IHelloService {

             
          public String sayHello(String username);
             
          }


          HelloServiceImpl.java
          package bing.server;

          import javax.jws.WebService;

          /**
          * <p>
          * WebService實現(xiàn)類
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          @WebService(endpointInterface
          = "bing.server.IHelloService", serviceName = "HelloService")
          public class HelloServiceImpl implements IHelloService {

              @Override
             
          public String sayHello(String username) {
                 
          return "hello, " + username;
              }


          }


          HelloServiceClient.java
          package bing.client;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import bing.server.IHelloService;

          /**
          * <p>
          * WebService調(diào)用方-客戶端
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          public class HelloServiceClient {

             
          public static void main(String[] args) {
                  ApplicationContext context
          = new ClassPathXmlApplicationContext("applicationContext-client.xml");
                  IHelloService helloService
          = (IHelloService) context.getBean("client");
                  String response
          = helloService.sayHello("Peter");
                  System.out.println(response);
              }


          }


          applicationContext-server.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <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.xsd
                                 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
          >
             
          <!--
                  ***注意***
                  手動添加的內(nèi)容:
                  xmlns:jaxws="http://cxf.apache.org/jaxws"
                  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
              
          -->
             
             
          <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" />

             
          <jaxws:endpoint id="helloService" implementor="bing.server.HelloServiceImpl" address="/helloService" />
                 
          </beans>

          applicationContext-client.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <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.xsd
                                 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
          >
             
          <!--
                  ***注意***
                  手動添加的內(nèi)容:
                  xmlns:jaxws="http://cxf.apache.org/jaxws"
                  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
              
          -->
             
             
          <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" />

             
          <bean id="client" class="bing.server.IHelloService" factory-bean="clientFactory" factory-method="create" />

             
          <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
                 
          <property name="serviceClass" value="bing.server.IHelloService" />
                 
          <property name="address" value="http://localhost:8080/CXFDemo/ws/helloService" />
             
          </bean>
          </beans>

          web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="3.0"
              xmlns
          ="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
          >
           
          <display-name>CXFDemo</display-name>
           
           
          <context-param>
               
          <param-name>contextConfigLocation</param-name>
               
          <param-value>classpath:applicationContext-server.xml</param-value>
           
          </context-param>

           
          <listener>
             
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
           
          </listener>
           
           
          <servlet>
             
          <servlet-name>CXFServlet</servlet-name>
             
          <display-name>CXFServlet</display-name>
             
          <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
             
          <load-on-startup>1</load-on-startup>
           
          </servlet>
           
          <servlet-mapping>
             
          <servlet-name>CXFServlet</servlet-name>
             
          <url-pattern>/ws/*</url-pattern>
           
          </servlet-mapping>
           
          <welcome-file-list>
             
          <welcome-file>index.jsp</welcome-file>
           
          </welcome-file-list>
          </web-app>

          所有項目都已配置完成,可以發(fā)布到Tomcat了,在瀏覽器中輸入:http://localhost:8080/CXFDemo/ws,返回如圖:


          從上圖中可以看到我們對外發(fā)布的WebService接口,點擊藍(lán)色超鏈接,返回如圖:

          到此,證明我們的Web Service已經(jīng)發(fā)布成功,可以進(jìn)行調(diào)用測試了。運行HelloServiceClient,返回如圖:


          全文完!


























          posted @ 2012-07-06 17:29 IceWee 閱讀(51163) | 評論 (27)編輯 收藏

          本演示例程是繼Java Tomcat SSL 服務(wù)端/客戶端雙向認(rèn)證(一),密鑰庫可證書的生成腳本不再重復(fù)黏貼,僅僅是用程序來代替瀏覽器訪問服務(wù)端。
          例程中使用到了Apache HttpClient庫,版本為4.1.3
          全部依賴庫:
          commons-logging-1.1.1.jar
          httpclient-4.1.3.jar
          httpcore-4.1.4.jar
          httpmime-4.1.3.jar(上傳文件使用)

          在(一)中的程序包中創(chuàng)建一個客戶端類:HttpsClient
          HttpsClient.java
          package com.icesoft.client;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.security.KeyStore;

          import org.apache.http.HttpEntity;
          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.conn.scheme.Scheme;
          import org.apache.http.conn.ssl.SSLSocketFactory;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.util.EntityUtils;

          public class HttpsClient {
             
             
          private static final String KEY_STORE_TYPE_JKS = "jks";
             
          private static final String KEY_STORE_TYPE_P12 = "PKCS12";
             
          private static final String SCHEME_HTTPS = "https";
             
          private static final int HTTPS_PORT = 8443;
             
          private static final String HTTPS_URL = "https://127.0.0.1:8443/HttpClientSSL/sslServlet";
             
          private static final String KEY_STORE_CLIENT_PATH = "E:/ssl/client.p12";
             
          private static final String KEY_STORE_TRUST_PATH = "E:/ssl/client.truststore";
             
          private static final String KEY_STORE_PASSWORD = "123456";
             
          private static final String KEY_STORE_TRUST_PASSWORD = "123456";

             
          public static void main(String[] args) throws Exception {
                  ssl();
              }

             
             
          private static void ssl() throws Exception {
                  HttpClient httpClient
          = new DefaultHttpClient();
                 
          try {
                      KeyStore keyStore 
          = KeyStore.getInstance(KEY_STORE_TYPE_P12);
                      KeyStore trustStore 
          = KeyStore.getInstance(KEY_STORE_TYPE_JKS);
                      InputStream ksIn
          = new FileInputStream(KEY_STORE_CLIENT_PATH);
                      InputStream tsIn
          = new FileInputStream(new File(KEY_STORE_TRUST_PATH));
                     
          try {
                          keyStore.load(ksIn, KEY_STORE_PASSWORD.toCharArray());
                          trustStore.load(tsIn, KEY_STORE_TRUST_PASSWORD.toCharArray());
                      }
          finally {
                         
          try { ksIn.close(); } catch (Exception ignore) {}
                         
          try { tsIn.close(); } catch (Exception ignore) {}
                      }

                      SSLSocketFactory socketFactory
          = new SSLSocketFactory(keyStore, KEY_STORE_PASSWORD, trustStore);
                      Scheme sch
          = new Scheme(SCHEME_HTTPS, HTTPS_PORT, socketFactory);
                      httpClient.getConnectionManager().getSchemeRegistry().register(sch);
                      HttpGet httpget
          = new HttpGet(HTTPS_URL);
                      System.out.println(
          "executing request" + httpget.getRequestLine());
                      HttpResponse response
          = httpClient.execute(httpget);
                      HttpEntity entity
          = response.getEntity();
                      System.out.println(
          "----------------------------------------");
                      System.out.println(response.getStatusLine());
                     
          if (entity != null) {
                          System.out.println(
          "Response content length: " + entity.getContentLength());
                          BufferedReader bufferedReader
          = new BufferedReader(new InputStreamReader(entity.getContent()));
                          String text;
                         
          while ((text = bufferedReader.readLine()) != null) {
                              System.out.println(text);
                          }

                          bufferedReader.close();
                      }

                      EntityUtils.consume(entity);
                  }
          finally {
                      httpClient.getConnectionManager().shutdown();
                  }

              }


          }



          啟動Tomcat,運行HttpsClient,控制臺返回:


          OK,和使用瀏覽器訪問得到的結(jié)果一模一樣!

          全文完!

          posted @ 2012-06-05 09:32 IceWee 閱讀(5228) | 評論 (1)編輯 收藏

               摘要: SSL——Secure Sockets Layer雙向認(rèn)證(個人理解):客戶端認(rèn)證:客戶端通過瀏覽器訪問某一網(wǎng)站時,如果該網(wǎng)站為HTTPS網(wǎng)站,瀏覽器會自動檢測系統(tǒng)中是否存在該網(wǎng)站的信任證書,如果沒有信任證書,瀏覽器一般會拒絕訪問,IE會有一個繼續(xù)訪問的鏈接,但地址欄是紅色,給予用戶警示作用,即客戶端驗證服務(wù)端并不是強(qiáng)制性的,可以沒有服務(wù)端的信任證書,當(dāng)然是否繼續(xù)訪問完全取...  閱讀全文

          posted @ 2012-06-04 17:36 IceWee 閱讀(32931) | 評論 (22)編輯 收藏

               摘要: 之前使用到了NIO的FileChannel做文件快速閱讀,后來發(fā)現(xiàn)存在一個巨大的BUG,使用它會一直不釋放文件句柄,即生成MD5的文件不能操作(移動或刪除等),這個BUG網(wǎng)上吵得沸沸揚揚,至今沒有解決,畢竟是SUN的BUG,解鈴還需系鈴人啊!咱只好乖乖的使用文件分塊讀取的方法,這種方式要求生成MD5和驗證的時候得使用相同的緩存大小。MD5Utils.javaCode highlighting pr...  閱讀全文

          posted @ 2012-06-01 17:57 IceWee 閱讀(3500) | 評論 (1)編輯 收藏

          這個類一般在記錄程序日志時可能會用到。
          ExceptionUtil.java
          /**
           * <p>
           * 異常工具類
           * </p>
           * 
           * 
          @author IceWee
           * @date 2012-4-19
           * 
          @version 1.0
           
          */

          public class ExceptionUtil {

              
          /**
               * <p>
               * 將異常堆棧信息以字符串的格式返回
               * </p>
               * 
               * 
          @param e 異常對象
               * 
          @return
               
          */

              
          public static String createStackTrackMessage(Exception e) {
                  StringBuffer messsage 
          = new StringBuffer();
                  
          if (e != null{
                      messsage.append(e.getClass()).append(
          "").append(e.getMessage()).append("\n");
                      StackTraceElement[] elements 
          = e.getStackTrace();
                      
          for (StackTraceElement stackTraceElement : elements) {
                          messsage.append(
          "\t").append(stackTraceElement.toString()).append("\n");
                      }

                  }

                  
          return messsage.toString();
              }

              
          }


          posted @ 2012-05-26 11:45 IceWee 閱讀(690) | 評論 (0)編輯 收藏

               摘要: 實際開發(fā)中可能會用到壓縮或解壓縮,底層借助于apache的zip,依賴jar文件:ant-1.7.1.jarZipUtilsTester.javaCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public static void&...  閱讀全文

          posted @ 2012-05-26 10:51 IceWee 閱讀(5618) | 評論 (1)編輯 收藏

               摘要: 本文中的Base64Utils.java在其他隨筆中已經(jīng)貼出。Java證書生成命令如下,不做過多解釋,可先到網(wǎng)上查詢下資料,本文僅提供工具類代碼:把生成的密鑰庫和證書都放到類的同包下。Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->keytool&nb...  閱讀全文

          posted @ 2012-05-21 17:14 IceWee 閱讀(15754) | 評論 (8)編輯 收藏

               摘要: 該工具類中用到了BASE64,需要借助第三方類庫:javabase64-1.3.1.jar注意:RSA加密明文最大長度117字節(jié),解密要求密文最大長度為128字節(jié),所以在加密和解密的過程中需要分塊進(jìn)行。RSA加密對明文的長度是有限制的,如果加密數(shù)據(jù)過大會拋出如下異常:Code highlighting produced by Actipro CodeHighlighter (freeware)ht...  閱讀全文

          posted @ 2012-05-19 16:54 IceWee 閱讀(42697) | 評論 (7)編輯 收藏

               摘要: 之前寫了DES加解密,AES幾乎與之相同,不同的是底層key的位數(shù)而已,不過這些對于我們使用者都是透明的。AESUtils.javaCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->package demo.security;import&nb...  閱讀全文

          posted @ 2012-05-19 13:43 IceWee 閱讀(11862) | 評論 (2)編輯 收藏

               摘要: 本工具類經(jīng)過測試可用,之前寫的沒有使用CipherInputStream和CipherOutputStream,生成的加密文件與源文件大小不一致,加密時沒有問題,解密時總是拋出如下異常:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Exception...  閱讀全文

          posted @ 2012-05-19 13:19 IceWee 閱讀(30907) | 評論 (3)編輯 收藏

          僅列出標(biāo)題
          共11頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 
          主站蜘蛛池模板: 武鸣县| 康定县| 团风县| 百色市| 大丰市| 东山县| 焦作市| 昌乐县| 白玉县| 西平县| 安福县| 宣化县| 福海县| 天气| 德阳市| 德兴市| 贡山| 芜湖县| 金昌市| 门源| 合肥市| 兴安县| 彰化县| 将乐县| 新干县| 内乡县| 蒙阴县| 通海县| 承德县| 耿马| 工布江达县| 绍兴县| 桦南县| 宜兴市| 梅河口市| 龙江县| 吴忠市| 大姚县| 太康县| 响水县| 敦煌市|