【问题标题】:IllegalStateException: Populating ehCache with JSON Object via HttpClient using POSTIllegalStateException:使用 POST 通过 HttpClient 使用 JSON 对象填充 ehCache
【发布时间】:2012-12-06 23:55:00
【问题描述】:

我正在使用 Java 6、Tomcat 7、Jersey、HttpClient 和 ehCache 服务器。

在$CATALINA_HOME/ehcache/WEB-INF/classes/ehcache.xml中注册了一个名为ipad的新缓存

<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();
    }
}

这是我得到的例外:

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 命令检索它。

这很难做到吗?我缺少什么(在实现、配置等方面)?

感谢您抽出宝贵时间阅读本文...

【问题讨论】:

    标签: java json jersey httpclient ehcache


    【解决方案1】:

    你有:

    http//...

    应该是:

    http://...

    你错过了“:”

    【讨论】:

    • 感谢 Chris,但在通过添加 :(冒号)修复 URL 后,我得到了这个新异常:线程“main”中的异常 java.lang.RuntimeException:失败:HTTP 错误代码:405在 com.myapp.client.EhCacheClientHelper.getEhCacheJsonResponseString(EhCacheClientHelper.java:27) 在 com.myapp.client.EhCachePostClientApp.main(EhCachePostClientApp.java:21)
    • Http 405 表示:方法不允许。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多