Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
Java 如何在springmvc中解码Gzip压缩请求体_Java_Spring_Http_Rest_Spring Mvc - Fatal编程技术网

Java 如何在springmvc中解码Gzip压缩请求体

Java 如何在springmvc中解码Gzip压缩请求体,java,spring,http,rest,spring-mvc,Java,Spring,Http,Rest,Spring Mvc,我有一个客户端,它通过 CONTENT-ENCODING deflate 我有这样的代码 @RequestMapping(value = "/connect", method = RequestMethod.POST) @ResponseBody public Map onConnect(@RequestBody String body){} 目前,“body”打印出了乱七八糟的压缩数据。 有什么方法可以让Spring MVC自动解压吗?您不能在Spring中处理它。取而代之的是使用一个过滤

我有一个客户端,它通过

CONTENT-ENCODING deflate
我有这样的代码

@RequestMapping(value = "/connect", method = RequestMethod.POST)
@ResponseBody
public Map onConnect(@RequestBody String body){}
目前,“body”打印出了乱七八糟的压缩数据。
有什么方法可以让Spring MVC自动解压吗?

您不能在Spring中处理它。取而代之的是使用一个过滤器,这样数据到达Spring时就已经被压缩了

希望这两个链接可以让你开始


这应该由服务器处理,而不是由应用程序处理

据我所知,Tomcat不支持它,尽管您可能会编写一个过滤器


处理这种情况的一种常见方法是将Tomcat(或您正在使用的任何Java容器)放在配置为处理压缩请求正文的Apache服务器后面。

您需要编写自己的过滤器来解压缩gzip请求正文。 因为您将从请求中读取整个输入流,所以您也需要覆盖参数解析方法。 这是我在代码中使用的过滤器。仅支持gzip的POST请求,但如果需要,可以将其更新为使用其他类型的请求。 还要注意解析参数我正在使用guava库,您可以从这里获取您的参数:

公共类GzipBodyDecompressFilter扩展了过滤器{
@凌驾
public void init(FilterConfig FilterConfig)抛出ServletException{
}
/**
*分析可能的gzip主体的servlet请求。
*当内容编码头具有“gzip”值且请求方法为POST时,我们读取所有
*gzip流和它是否有任何数据解压。如果gzip内容编码头
*指定,但正文实际上不是gzip格式,我们将抛出ZipException。
*
*@param servletRequest servlet request
*@param servletResponse servlet response
*@param-chain过滤器链
*@throws IOException失败时抛出
*@在失败时抛出ServletException
*/
@凌驾
公共最终作废doFilter(最终ServletRequest ServletRequest,
最终ServletResponse ServletResponse,
最终筛选器链)抛出IOException、ServletException{
HttpServletRequest=(HttpServletRequest)servletRequest;
HttpServletResponse=(HttpServletResponse)servletResponse;
布尔值isgzip=request.getHeader(HttpHeaders.CONTENT\u编码)!=null
&&getHeader(HttpHeaders.CONTENT_ENCODING).contains(“gzip”);
boolean requestTypeSupported=HttpMethods.POST.equals(request.getMethod());
if(支持isgzip&!RequestTypes){
抛出新的IllegalStateException(request.getMethod())
+“不支持gzip参数体。”
+“当前仅支持POST请求。”);
}
if(支持isgzip&请求类型){
请求=新的GzippedInputStreamWrapper((HttpServletRequest)servletRequest);
}
链式过滤器(请求、响应);
}
/**
*@doc
*/
@凌驾
公开最终作废销毁(){
}
/**
*包装类,用于检测请求是否已gzip并将其解压缩。
*/
最后一个类GzippedInputStreamWrapper扩展了HttpServletRequestWrapper{
/**
*解析post参数时使用的默认编码。
*/
公共静态最终字符串默认值_ENCODING=“ISO-8859-1”;
/**
*序列化字节数组,它是解压缩Gzip正文的结果。
*/
专用字节[]字节;
/**
*构造一个封装给定请求的请求对象。
*如果内容编码包含“gzip”,我们将输入流包装到字节数组中
*原始输入流中没有任何内容,但hew包装的输入流始终返回
*可复制的未压缩输入流。
*
*@param请求将包装哪个输入流。
*@在输入流请求失败时引发java.io.IOException。
*/
公共GzippedInputStreamWrapper(最终HttpServletRequest请求)引发IOException{
超级(请求);
试一试{
final InputStream in=new gzip输入流(request.getInputStream());
字节=ByteStreams.toByteArray(in);
}捕获(EOFEException e){
字节=新字节[0];
}
}
/**
*@return可复制的输入流,该输入流等于初始servlet输入
*流(如果未压缩)或返回解压缩的输入流。
*@失败时抛出IOException。
*/
@凌驾
公共ServletiInputStream getInputStream()引发IOException{
final ByteArrayInputStream sourceStream=新的ByteArrayInputStream(字节);
返回新的ServletInputStream(){
public int read()引发IOException{
返回sourceStream.read();
}
public void close()引发IOException{
super.close();
sourceStream.close();
}
};
}
/**
*需要重写getParametersMap,因为我们最初读取整个输入流并
*servlet容器将无法访问输入流数据。
*
*@return已解析参数列表。仅当内容类型为
*设置了“application/x-www-form-urlencoded”。
*/
@凌驾
公共映射getParameterMap(){
字符串contentEncodingHeader=getHeader(HttpHeaders.CONTENT_类型);
如果(!Strings.isNullOrEmpty(contentEncodingHeader)
&&contentEncodingHeader.contains(“application/x-www-form-urlencoded”)){
Map params=newhashmap(super.getParameterMap());