使用MockRestServiceServer进行Gzip压缩

时间:2016-06-09 09:50:29

标签: java junit compression gzip resttemplate

我有一个基于 Spring 3.2.0 构建的Java应用程序,执行对提供JSON数据的REST api的外部调用。

调用由Spring RestTemplate 类执行, Jackson 2.2.3作为序列化器/反序列化器。

该调用是函数式的,支持普通和gzip响应。

为了Junit测试呼叫,我使用 MockRestServiceServer 。一切顺利,直到我尝试引入gzip压缩。我无法在官方文档中找到如何在MockRestServiceServer中激活gzip压缩,所以我去了手动路线:

  • 手动gzip响应的字符串内容

  • 设置" Content-Encoding"到" gzip"在标题中

不幸的是,我在反序体化反序体时抛出了同样的错误:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Illegal character ((CTRL-CHAR, code 31)): 
only regular white space (\r, \n, \t) is allowed between tokens
at [Source: java.io.ByteArrayInputStream@110d68a; line: 1, column: 2];
nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): 
only regular white space (\r, \n, \t) is allowed between tokens

以下是当前代码(由于公司数据而重新设计......)

测试类

public class ImportRefCliCSTest {

    @Autowired
    private MyService myService;

    private MockRestServiceServer mockServer;

    @Before
    public void before() {
        mockServer = MockRestServiceServer.createServer(myService.getRestTemplate());
    }

    @Test
    public void testExternalCall() throws IOException {

        String jsonData = "[{\"testing\":\"Hurray!\"}]";
        HttpHeaders headers = new HttpHeaders();
        headers.add( "Content-Encoding", "gzip" );

        DefaultResponseCreator drc = withSuccess( gzip( jsonData ),
            MediaType.APPLICATION_JSON ).headers( headers );

        mockServer.expect( requestTo( myService.EXTERNAL_CALL_URL ) )
            .andExpect( method( HttpMethod.GET ) ).andRespond(drc);

        myService.performCall();
    }


    private static String gzip(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        String outStr = out.toString();
        return outStr;
    }
}

服务类

@Service
public class MyService {

    public static final String EXTERNAL_CALL_URL = "<myURL>";
    private RestTemplate restTemplate;

    {
        restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create().build()));
    }


    public void performCall() {

        try {
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.add("Accept-Encoding", "gzip");
            HttpEntity<MyObject[]> requestEntity = new HttpEntity<MyObject[]>(requestHeaders);

            ResponseEntity<MyObject[]> responseEntity = restTemplate.exchange(
                EXTERNAL_CALL_URL, HttpMethod.GET, requestEntity, MyObject[].class);
            MyObject[] array = responseEntity.getBody();
            if (array == null || array.length == 0) {
                return null;
            }
            return null;
        } catch (RestClientException e) {
            return null;
        }
    }

    public RestTemplate getRestTemplate(){
        return restTemplate;
    }
}

我觉得我错过了什么。手动gzip压缩看起来很可疑。

有没有人对此有所了解?

提前感谢您的回答!

1 个答案:

答案 0 :(得分:0)

当程序将gzip内容转换为字符串时,会折叠一些字节。因此客户端无法解压缩并抛出异常。解决方案是返回byte[]

private static byte[] gzip(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return new byte[0];
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());//consider to use str.getBytes("UTF-8")
    gzip.close();
    return out.toByteArray();
}