2012-12-06 32 views
1

正在使用Java 6,Tomcat 7,Jersey,HttpClient和ehCache服務器。IllegalStateException:使用POST通過HttpClient使用JSON對象填充ehCache

以iPad的$ CATALINA_HOME/ehcache的/ WEB-INF /班/ ehcache.xml中內部的名稱註冊一個新的緩存

<cache name="ipad" 
      maxElementsInMemory="10000" 
      eternal="true" 
      overflowToDisk="false" 
      memoryStoreEvictionPolicy="FIFO" 
/> 

這是我的客戶是什麼樣子:

import org.apache.http.impl.client.DefaultHttpClient; 

public class EhCachePostClient { 
    public static void main(String[] args) throws Exception { 
      String jsonString = "{\"qty\":100,\"name\":\"iPad 4\"}"; 

      // Post the same object to ehCache 
      DefaultHttpClient ehCacheClient = new DefaultHttpClient(); 
      String ehCacheResponseString = 
    EhCacheClientHelper.getEhCacheJsonResponseString(ehCacheClient, jsonString); 
      System.out.println("\nFrom ehCache Server: " + ehCacheResponseString); 
      ehCacheClient.getConnectionManager().shutdown(); 
    } 
} 

下面是EhCacheClientHelper類(其中大部分工作已完成):

import java.io.BufferedReader; 
import java.io.InputStreamReader; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 

public class EhCacheClientHelper { 

    private static final String EHCACHE_IPAD_URI 
        = "http//localhost:8080/ehcache/rest/ipad"; 

    public static String getEhCacheJsonResponseString(
       DefaultHttpClient httpClient, String responseString) 
    throws Exception { 
     HttpPost postRequest = new HttpPost(EHCACHE_MDS_URI); 

     StringEntity input = new StringEntity(responseString); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     if (response.getStatusLine().getStatusCode() != 201) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + response.getStatusLine().getStatusCode()); 
     } 

     BufferedReader br = new BufferedReader(
       new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     StringBuilder ehCacheOutput = new StringBuilder(); 
     System.out.println("\nOutput from ehCache Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      ehCacheOutput.append(output); 
     } 
     return ehCacheOutput.toString(); 
    } 
} 

這是例外情況I得到:

Exception in thread "main" java.lang.IllegalStateException: Target host must not be null, or set in parameters. 
    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:784) 
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:414) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) 
    at com.myapp.client.EhCacheClientHelper.getEhCacheJsonResponseString(EhCacheClientHelper.java:22) 
    at com.myapp.client.EhCachePostClient.main(EhCachePostClient.java:19) 

我在做什麼錯?

我想要做的就是在ehCache中創建一個樣本緩存,向其發佈一些類型的JSON對象,然後使用curl命令檢索它。

這是一件很難做的事嗎?我錯過了什麼(在實施,配置等方面)?

感謝您在百忙之中閱讀本文時...

回答

2

您有:

的http // ...

應該是:

http:// ...

你錯過了:「

+0

感謝克里斯,但通過添加在:(冒號)修復URL後,我得到這個新的異常:線程」main「中的異常java.lang.RuntimeException:失敗:HTTP錯誤代碼:405 \t在com.myapp.client.EhCacheClientHelper.getEhCacheJsonResponseString(EhCacheClientHelper.java:27) \t在com.myapp.client.EhCachePostClientApp.main(EhCachePostClientApp.java:21) –

+0

HTTP 405手段:方法不允許的。 – chris