Gzip Servlet Filter Causing JSON parse error

问题内容:

I have implemented a Gzip Compression filter in servlet filter level. My front end jsp is making ajax call to server side to retrieve json response.

In jsp client, here is the snippet of code:

var _url = (xxx.contextPath + '/documenttype/getextension/'+ val);
    $.getJSON(_url).done(function (data) {
        if(data){
            $('#documents').attr('accept', data);
        }
    }).fail(function( jqxhr, textStatus, error ) {
        var err = textStatus + ", " + error;
        alert(err);
    });

In server side spring controller, here is the snippet of code:

@ResponseBody
@RequestMapping(value = "/getextension/{typeId}", method = 
RequestMethod.GET)
public String findExtension(@PathVariable Long typeId) {
    String ext = getEntityService().findExtension(typeId);
    // json requires string with double quotes
    ext = new StringBuilder("\"").append(ext).append("\"").toString();
    return ext;
}

I did a debug and the above method will return “.pdf” value as json response. However in front end, i encounter “parsererror, SyntaxError: Unexpected end of JSON input” error. I have other ajax call which return full json object is working well with the gzip compression, eg:

[{"id":40,"createdBy":1,"createdDate":"21/11/2017 
12:49","updatedBy":1,"updatedDate":"21/11/2017 12:49","version":0,"group":
{"id":23,"createdBy":1,"createdDate":"21/11/2017 
12:49","updatedBy":1,"updatedDate":"21/11/2017 
12:49","version":0,"code":"ALTDISPUTERES","name":"Alternative Dispute 
Resolution","store":
{},"systemGenerated":false,"position":0,"displayAll":false,"handler":.....

The gzip compression is also working well with other types like normal page response, js file and etc, may i know what is the problem?

Chrome debugger Screen

问题评论:

原文地址:

https://stackoverflow.com/questions/47748210/gzip-servlet-filter-causing-json-parse-error

添加评论

友情链接:蝴蝶教程