springboot による圧縮リクエストの処理
最近、帯域幅を節約するために、UnionPay の要件を満たすようにパケットを圧縮する必要があります。ただし、springboot に付属の圧縮設定を使用しても機能しません。
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: 圧縮を有効にするかどうかを示します。デフォルトで有効になっています。true: 有効、false: 無効
server.compression .mime-types: xml、json、html、その他の形式を含む圧縮コンテンツのタイプ
server.compression.compressionMinSize: 圧縮データの最小長をバイト単位で有効にします。デフォルトは 2048 バイトです
ソース コードは次のとおりです:
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; }
1. Tomcat 設定の圧縮原理
Tomcat の圧縮は、応答メッセージを圧縮することです。リクエストに accept-encoding が存在する場合は、ヘッダーで Tomcat が圧縮を設定すると、応答時にデータが圧縮されます。
Tomcat 圧縮ソース コードは 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; } }
2. UnionPay メッセージ圧縮
カード発行会社として、UnionPay メッセージ要求メッセージは圧縮され、メッセージ ヘッダーはありませんの accept-encoding フィールドを使用するため、圧縮と解凍に Tomcat 設定を直接使用することはできません。
この種のリクエストは個別に処理する必要があります
@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); } }
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; } }
補足: Java springbooot は gzip を使用して文字列を圧縮します
import lombok.extern.slf4j.Slf4j; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * effect:压缩/解压 字符串 */ @Slf4j public class CompressUtils { /** * effect 使用gzip压缩字符串 * @param str 要压缩的字符串 * @return */ public static String compress(String str) { if (str == null || str.length() == 0) { return str; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = null; try { gzip = new GZIPOutputStream(out); gzip.write(str.getBytes()); } catch (IOException e) { log.error("",e); } finally { if (gzip != null) { try { gzip.close(); } catch (IOException e) { log.error("",e); } } } return new sun.misc.BASE64Encoder().encode(out.toByteArray()); // return str; } /** * effect 使用gzip解压缩 * * @param str 压缩字符串 * @return */ public static String uncompress(String str) { if (str == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = null; GZIPInputStream ginzip = null; byte[] compressed = null; String decompressed = null; try { compressed = new sun.misc.BASE64Decoder().decodeBuffer(str); in = new ByteArrayInputStream(compressed); ginzip = new GZIPInputStream(in); byte[] buffer = new byte[1024]; int offset = -1; while ((offset = ginzip.read(buffer)) != -1) { out.write(buffer, 0, offset); } decompressed = out.toString(); } catch (IOException e) { log.error("",e); } finally { if (ginzip != null) { try { ginzip.close(); } catch (IOException e) { } } if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return decompressed; } }
以上がspringboot が圧縮リクエストを処理する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

jvmmanagesgarbagecollectionacrossplatformseftivivivivitybyusagenerationalaphadadadaptingtosandhardwaredefferences.itemployscollectorslikeserial、parallel、cms、andg1、各sutitedfordifferentscenarios

Javaは、Javaの「Write and Averywherewhere」という哲学がJava Virtual Machine(JVM)によって実装されているため、変更なしで異なるオペレーティングシステムで実行できます。コンパイルされたJavaバイトコードとオペレーティングシステムの間の仲介者として、JVMはバイトコードを特定のマシン命令に変換し、JVMがインストールされた任意のプラットフォームでプログラムが独立して実行できることを確認します。

Javaプログラムの編集と実行は、BytecodeとJVMを通じてプラットフォームの独立性を達成します。 1)Javaソースコードを書き、それをbytecodeにコンパイルします。 2)JVMを使用して、任意のプラットフォームでByteCodeを実行して、コードがプラットフォーム間で実行されるようにします。

Javaのパフォーマンスはハードウェアアーキテクチャと密接に関連しており、この関係を理解することでプログラミング機能を大幅に改善できます。 1)JVMは、CPUアーキテクチャの影響を受けるJITコンピレーションを介して、Java Bytecodeを機械命令に変換します。 2)メモリ管理とゴミ収集は、RAMとメモリバスの速度の影響を受けます。 3)キャッシュとブランチ予測Javaコードの実行を最適化します。 4)マルチスレッドと並列処理がマルチコアシステムのパフォーマンスを改善します。

ネイティブライブラリを使用すると、これらのライブラリはオペレーティングシステムごとに個別にコンパイルする必要があるため、Javaのプラットフォームの独立性が破壊されます。 1)ネイティブライブラリはJNIを介してJavaと対話し、Javaが直接実装できない機能を提供します。 2)ネイティブライブラリを使用すると、プロジェクトの複雑さが増し、さまざまなプラットフォームのライブラリファイルの管理が必要です。 3)ネイティブライブラリはパフォーマンスを改善できますが、それらは注意して使用し、クロスプラットフォームテストを実施する必要があります。

JVMは、JavanativeInterface(JNI)およびJava Standard Libraryを介してオペレーティングシステムのAPIの違いを処理します。1。JNIでは、Javaコードがローカルコードを呼び出し、オペレーティングシステムAPIと直接対話できます。 2. Java Standard Libraryは統一されたAPIを提供します。これは、異なるオペレーティングシステムAPIに内部的にマッピングされ、コードがプラットフォーム間で実行されるようにします。

modularitydoesnotdirectlyectlyectjava'splatformindepensence.java'splatformendepenceismaindainededainededainededaindainedaindained bythejvm、butmodularityinfluencesApplucationStructure andmanagement、間接的なインパクチャプラット形成依存性.1)

bytecodeinjavaisthe intermediaterepresentationthateNablesplatformindepence.1)javacodeis compiledintobytecodestoredin.classfiles.2)thejvminterpretsorcompilesthisbytecodeintomachinecodeatime、


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ホットトピック









