当标题看起来正确时,为什么Springframework会返回一个空体?

时间:2012-11-29 16:44:42

标签: java android spring

当我运行此代码时,我在httpResponse中获得响应。它报告了标头中正确的字节数。但身体是空的。当我调用.getBody()时它是空的。

标题:

<200 OK,{Date=[Thu, 29 Nov 2012 16:26:06 GMT], Server=[Apache], Vary=[Accept-Encoding], Content-Length=[5072], Keep-Alive=[timeout=10, max=100], Connection=[Keep-Alive], Content-Type=[text/html]}>

我做错了什么?

    String url = new String("http://www.myurl.com/scripts/json/v1/slipmanager.php");

    MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
    formData.add("username", userName);
    formData.add("password", md5(userPassword));
    formData.add("method", "getslips");

    RestTemplate template = new RestTemplate(true);
    template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    HttpHeaders requestHeaders = new HttpHeaders();

    requestHeaders.setContentType(new MediaType("multipart", "form-data"));
    template.getMessageConverters().add(new StringHttpMessageConverter());

    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
    ResponseEntity<?> httpResponse = null;
    try
    {
        httpResponse = template.exchange(url, HttpMethod.POST, request, null);
        String tmp = (String) httpResponse.getBody();
                    //THIS IS WHERE THE BODY IS NULL
    }
    catch (Exception e) 
    {
        Log.e("POST", e.getMessage(), e);
    }

1 个答案:

答案 0 :(得分:3)

您要求回复类型为“null”,因此Spring认为您要丢弃该正文。如果您希望它是一个String,String.class应该可以工作。据我所知,你甚至不需要显式的StringHttpMessageConverter。