Rumah >Java >javaTutorial >Cara springboot mengendalikan permintaan mampatan
Baru-baru ini, untuk menjimatkan lebar jalur, paket perlu dimampatkan untuk memenuhi keperluan UnionPay. Tetapi menggunakan tetapan mampatan yang disertakan dengan springboot tidak berfungsi:
server.compression.enabled=true server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain server.compression.compressionMinSize=10
server.compression.enabled: Menunjukkan sama ada hendak menghidupkan mampatan, yang dihidupkan secara lalai, benar: dihidupkan, palsu: tidak dihidupkan
server.compression .mime-types: Jenis kandungan termampat, termasuk xml, json, html dan format lain
server.compression.compressionMinSize: Dayakan panjang minimum data termampat, dalam bait, lalai ialah 2048 bait
Kod sumber adalah seperti berikut:
public class Compression { private boolean enabled = false; private String[] mimeTypes = new String[]{"text/html", "text/xml", "text/plain", "text/css", "text/javascript", "application/javascript", "application/json", "application/xml"}; private String[] excludedUserAgents = null; private int minResponseSize = 2048; }
Mampatan Tomcat adalah untuk memampatkan mesej respons apabila pengekodan terima hadir dalam pengepala permintaan, jika Tomcat menetapkan pemampatan, ia akan Memampatkan data pada tindak balas.
Kod sumber mampatan tomcat ditetapkan dalam Http11Processor:
public class Http11Processor extends AbstractProcessor { private boolean useCompression() { // Check if browser support gzip encoding MessageBytes acceptEncodingMB = request.getMimeHeaders().getValue("accept-encoding"); if ((acceptEncodingMB == null)-->当请求头没有这个字段是不进行压缩 || (acceptEncodingMB.indexOf("gzip") == -1)) { return false; } // If force mode, always compress (test purposes only) if (compressionLevel == 2) { return true; } // Check for incompatible Browser if (noCompressionUserAgents != null) { MessageBytes userAgentValueMB = request.getMimeHeaders().getValue("user-agent"); if(userAgentValueMB != null) { String userAgentValue = userAgentValueMB.toString(); if (noCompressionUserAgents.matcher(userAgentValue).matches()) { return false; } } } return true; } }
Sebagai pengeluar kad, mesej permintaan mesej UnionPay dimampatkan, dan pengepala mesej Terdapat tiada medan pengekodan terima dalam fail, dan anda tidak boleh terus menggunakan konfigurasi tomcat untuk pemampatan dan penyahmampatan.
Permintaan jenis ini perlu dikendalikan secara berasingan
@RestController @RequestMapping("/user") public class UserController { private static final Logger logger = LoggerFactory.getLogger(UserController.class); /** * * application/xml格式报文 * */ @PostMapping(path = "/test", produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE) public void getUserInfoById(HttpServletRequest request, HttpServletResponse response) throws IOException { String requestBody; String resultBody="hello,wolrd"; byte[] returnByte; if (StringUtils.isNoneEmpty(request.getHeader("Content-encoding"))) { logger.info("报文压缩,需要进行解压"); //业务处理 //返回报文也同样需要进行压缩处理 assemleResponse(request,response,resultBody); } } public static void assemleResponse(HttpServletRequest request, HttpServletResponse response,String resultBody) throws IOException { response.setHeader("Content-Type","application/xml;charset=UTF-8"); response.setHeader("Content-Encoding","gzip"); byte[] returnByte=GzipUtil.compress(resultBody); OutputStream outputStream=response.getOutputStream(); outputStream.write(returnByte); } }rrree
public class GzipUtil { public static String uncompress(byte[] bytes){ if (bytes == null || bytes.length == 0) { return null; } String requestBody=null; ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes); GZIPInputStream gzipInputStream = null; try { gzipInputStream = new GZIPInputStream(byteArrayInputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int temp; while ((temp = gzipInputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, temp); } requestBody = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } return requestBody; } public static String uncompress(HttpServletRequest request){ String requestBody=null; int length = request.getContentLength(); try { BufferedInputStream bufferedInputStream = new BufferedInputStream(request.getInputStream()); GZIPInputStream gzipInputStream = new GZIPInputStream(bufferedInputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int temp; while ((temp = gzipInputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, temp); } requestBody = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } return requestBody; } public static byte[] compress(String src){ if (src == null || src.length() == 0) { return null; } ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); try { GZIPOutputStream gzipOutputStream=new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(src.getBytes(StandardCharsets.UTF_8)); gzipOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } byte[] bytes=byteArrayOutputStream.toByteArray(); return bytes; } }
Atas ialah kandungan terperinci Cara springboot mengendalikan permintaan mampatan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!