Apache HttpClient 4.0.1- HttpPut由于输入结束而没有要映射的内容\ n错误

时间:2018-06-14 04:38:46

标签: java rest groovy apache-httpclient-4.x spring-data-rest

我有一个Spring restful api,我正在尝试使用HttpPut方法和Apache HttpClient 4.0.1进行更新而不是

import groovy.json.JsonBuilder
import org.apache.http.client.methods.HttpPut
import org.apache.http.entity.StringEntity
import groovy.json.JsonSlurper
import org.apache.commons.io.output.ByteArrayOutputStream;

def httpConnector = applicationContext.getBean('httpConnector')
String apiURL = "https://localhost:7443/api/ruleConfigurations/29"
def putRequest = new HttpPut(apiURL);

def testJson = new JsonBuilder()
root = testJson name: "test_name", displayName: "test_display_name", active: "false" , value: "test_value"

println "testJson = "+testJson.toString()
def stringEntity = new StringEntity(testJson.toString());  
putRequest.setHeader("Accept", "application/json");
putRequest.setHeader("Content-type", "application/json");           


def httpClient = httpConnector.getHttpClient()
def putResponse = httpClient.execute(putRequest)
def resCode = putResponse.getStatusLine().getStatusCode()

  if(resCode==200){
    println "succesfully updated Rule config"
  }
else { 
  println "Response Code = ${resCode} Error in updating Rule config"
  println "error message = "+getResponseData(putResponse)  
    }
def getResponseData(def response) throws java.io.IOException {
        byte[] responseData
        InputStream responseStream = response.getEntity().getContent()
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
            byteArrayOutputStream.write(responseStream)
            responseData = byteArrayOutputStream.toByteArray()
        } finally {
            responseStream.close()
        }
        return new String(responseData,'utf-8')
   }    

以下是httpConnector.getHttpClient()

的方法
 private DefaultHttpClient getHttpClient() {
        if (httpClient != null) return httpClient;
        final int MAX_CONNECTIONS = 300;
        CookieStore cookieStore = new BasicCookieStore();
        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params,MAX_CONNECTIONS);
        ConnManagerParams.setTimeout(params, 20000);
        HttpConnectionParams.setSoTimeout(params, 240000);
        HttpConnectionParams.setConnectionTimeout(params,30000);
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_CONNECTIONS);
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080));
        schemeRegistry.register(new Scheme("https", getSockectFactory(), 8443));
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        def httpClient = new DefaultHttpClient(cm, params);
        return httpClient;
    }

以下是尝试对restful api进行更新时获得的输出

testJson = {"name":"test_name","displayName":"test_display_name","active":"false","value":"test_value"} 
Response Code = 400 Error in updating Rule config 
error message = {
   "cause":{
      "cause":null,
      "message":"No content to map due to end-of-input\n at [Source: org.apache.catalina.connector.CoyoteInputStream@5438c5f7; line: 1, column: 0]"
   },
   "message":"Could not read an object of type class com.validation.entity.RuleConfiguration from the request!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input\n at [Source: org.apache.catalina.connector.CoyoteInputStream@5438c5f7; line: 1, column: 0]"
}   

如果我在邮递员中对testJson使用相同的HttpPut,则可以正常使用

enter image description here

我不确定为什么No content to map due to end-of-input\n HttpPut导致Apache HttpClient 4.0.1出现export CI=1错误,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在您显示的示例中,您没有打电话

putRequest.setEntity(stringEntity)

所以你发送的请求是空的。