Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String RestTemplate postForObject响应被截断_String_Spring_Resttemplate - Fatal编程技术网

String RestTemplate postForObject响应被截断

String RestTemplate postForObject响应被截断,string,spring,resttemplate,String,Spring,Resttemplate,尝试使用SpringWeb客户端获取响应,但由于响应大小大于string类的大小,结果响应被截断。有没有其他方法可以在不被截断的情况下获得响应 LinkedMultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>(); //String response = ""; try{ File tempFile = F

尝试使用SpringWeb客户端获取响应,但由于响应大小大于string类的大小,结果响应被截断。有没有其他方法可以在不被截断的情况下获得响应

LinkedMultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>();
        //String response = "";
        try{
            File tempFile = File.createTempFile("ccda", "File");
            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copy(ccdaFile.getInputStream(), out);
            requestMap.add("ccdaFile", new FileSystemResource(tempFile));       
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = 
                                        new HttpEntity<LinkedMultiValueMap<String, Object>>(requestMap, headers);
            RestTemplate restTemplate = new RestTemplate();
            FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
            formConverter.setCharset(Charset.forName("UTF8"));
            restTemplate.getMessageConverters().add(formConverter);
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

            String response = restTemplate.postForObject("localhost:8080", 
                                                requestEntity, String.class);

            tempFile.delete();
        }catch(Exception exc)
        {
            exc.printStackTrace();
        }
LinkedMultiValueMap requestMap=新建LinkedMultiValueMap();
//字符串响应=”;
试一试{
File tempFile=File.createTempFile(“ccda”,“文件”);
FileOutputStream out=新的FileOutputStream(tempFile);
复制(ccdaFile.getInputStream(),out);
add(“ccdaFile”,新文件系统资源(tempFile));
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
HttpEntity请求实体=
新的HttpEntity(requestMap、headers);
RestTemplate RestTemplate=新RestTemplate();
FormHttpMessageConverter formConverter=新FormHttpMessageConverter();
formConverter.setCharset(Charset.forName(“UTF8”);
restemplate.getMessageConverters().add(formConverter);
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
String response=restemplate.postForObject(“localhost:8080”,
requestEntity,String.class);
tempFile.delete();
}捕获(异常exc)
{
exc.printStackTrace();
}
您可以使用InputStream 我想你可以这样做:

InputStream is = rt.execute("localhost:8080", HttpMethod.POST, requestCallback, responseExtractor);
其中requestCallback是org.springframework.web.client.requestCallback的一个实现

public class MyRequestCallback implements RequestCallback
{
    @Override
    public void doWithRequest(ClientHttpRequest request) throws IOException
    {
        request.getHeaders().add(HttpHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE);
        request.getHeaders().add(HttpHeaders.ACCEPT, MimeTypeUtils.APPLICATION_JSON_VALUE);
    }
}
而responseExtractor可能由此类的实例执行

public class MyResponseExtractor implements ResponseExtractor<InputStream>
{
    @Override
    public Boolean extractData(ClientHttpResponse response) throws IOException
    {
        HttpStatus status = response.getStatusCode();
        switch (status)
        {
        case OK:
            return response.getBody();
        default:
            return null;
        }   
    }
}
公共类MyResponseExtractor实现ResponseExtractor
{
@凌驾
公共布尔提取数据(ClientHttpResponse响应)引发IOException
{
HttpStatus status=response.getStatusCode();
开关(状态)
{
案例OK:
返回response.getBody();
违约:
返回null;
}   
}
}
一旦获得InputStream,您就可以对其进行管理 希望它有用

您可以使用InputStream 我想你可以这样做:

InputStream is = rt.execute("localhost:8080", HttpMethod.POST, requestCallback, responseExtractor);
其中requestCallback是org.springframework.web.client.requestCallback的一个实现

public class MyRequestCallback implements RequestCallback
{
    @Override
    public void doWithRequest(ClientHttpRequest request) throws IOException
    {
        request.getHeaders().add(HttpHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE);
        request.getHeaders().add(HttpHeaders.ACCEPT, MimeTypeUtils.APPLICATION_JSON_VALUE);
    }
}
而responseExtractor可能由此类的实例执行

public class MyResponseExtractor implements ResponseExtractor<InputStream>
{
    @Override
    public Boolean extractData(ClientHttpResponse response) throws IOException
    {
        HttpStatus status = response.getStatusCode();
        switch (status)
        {
        case OK:
            return response.getBody();
        default:
            return null;
        }   
    }
}
公共类MyResponseExtractor实现ResponseExtractor
{
@凌驾
公共布尔提取数据(ClientHttpResponse响应)引发IOException
{
HttpStatus status=response.getStatusCode();
开关(状态)
{
案例OK:
返回response.getBody();
违约:
返回null;
}   
}
}
一旦获得InputStream,您就可以对其进行管理
希望它有用

inputStream不能用于jsonMapping,它正在为inputStream抛出json映射异常。@Vishwaksena您正在使用json吗?所以我想你可以直接使用对象,而不是用pasing来表示字符串;我使用了非常大的JSON,但从未出现过此问题。我正在尝试使用返回的响应,它是一个JSON,但将其转换为字符串会使其被截断。请在调试级别(或更好的跟踪)配置日志记录并重复调用,然后将日志消息放在此处。。也许我们可以获得更多信息。我实际上正在访问一个实时服务,因此保留了localhost而不是它,我无法获取服务器日志。inputStream不能用于jsonMapping,它正在为inputStream引发json映射异常。@Vishwaksena您正在使用json吗?所以我想你可以直接使用对象,而不是用pasing来表示字符串;我使用了非常大的JSON,但从未出现过此问题。我正在尝试使用返回的响应,它是一个JSON,但将其转换为字符串会使其被截断。请在调试级别(或更好的跟踪)配置日志记录并重复调用,然后将日志消息放在此处。。也许我们可以得到更多的信息。我实际上正在使用一个实时服务,因此保留了localhost而不是它,而且我无法获取服务器日志。