SpringBoot使用RequestBodyAdvice統一プロセスパラメータ

免責事項:この記事はCC 4.0 BY-SAの著作権契約書に従って、ブロガーオリジナル記事です、複製、元のソースのリンクと、この文を添付してください。
このリンクします。https://blog.csdn.net/xingbaozhen1210/article/details/98189562
実用的なプロジェクトでは、多くの場合、このようなフィルタパラメータ、文字エンコーディング、サードパーティの復号化などの要求のいくつかの統一動作パラメータを、実行する必要がありますなど、春は私たちのコントローラーで退屈なプロセスを排除し、RequestBodyAdviceにグローバルなソリューションを提供します。 

だけなのでGETメソッドを、その原則やAOPので、@RqestBodyノートを使用RequestBodyAdviceテイク効果は動作しません。

com.xbz.common.webパッケージ;
 
インポートorg.springframework.core.MethodParameter;
インポートorg.springframework.http.HttpHeaders;
インポートorg.springframework.http.HttpInputMessage;
インポートorg.springframework.http.converter.HttpMessageConverter;
インポートORG。 springframework.web.bind.annotation.ControllerAdvice;
インポートorg.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
 
インポートにjava.io.IOException;
インポートするjava.io.InputStream;
インポートjava.lang.reflect.Type。
 
/ **
 *グローバル要求ハンドラクラス・パラメータ@Title
 Xingbz @author *
 2019年8月2日* @createDate
 * /
ここでのセットが実行され、現在のフィールドのアドバイスを必要とする// @ControllerAdvice(basePackages = "com.xbz.controller")、世界的に影響を省略
{RequestBodyAdviceクラスGlobalRequestBodyAdvice実装パブリック
 
 
    / **ここではそれがfalseを返した場合、いない現在のビジネスのアドバイス* /実行するために
    @Overrideの
    サポート(MethodParameter MethodParameter、タイプたtargetType、クラス<?拡張HttpMessageConverter <?>> converterType){boolean型のパブリックを
// methodParameter.getMethodリターン()isAnnotationPresent(XXApiReq.class);.
        falseに復帰;
    }
 
    / **
     * @title読み取りパラメータを実行する前に
     * @descriptionこのコーディング/復号化を行う/動作パラメータオブジェクトをパッケージ
     *
     * * /
    @Override
    公共HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage、MethodParameterパラメータタイプたtargetType、クラス<?延びHttpMessageConverter <?>> converterType)IOExceptionが{スロー
        新しいXHttpInputMessage(inputMessage、 "UTF-8")を返します。
    }
 
    / **
     * @title读取参数后执行
     * @author Xingbz
     * /
    @Override
    パブリックオブジェクトafterBodyRead(オブジェクト本体、HttpInputMessage inputMessage、MethodParameterパラメータ、タイプたtargetType、クラス<?延びHttpMessageConverter <?>> converterType){
        戻りinputMessage ;
    }
 
    / **
     * @title无请求时的处理
     * /
    @Override
    パブリックオブジェクトhandleEmptyBody(オブジェクト本体、HttpInputMessage inputMessage、MethodParameterパラメータ、タイプたtargetType、クラス<延びHttpMessageConverter <>> converterType?){
        戻り体;
    }
}
 
//这里实现了HttpInputMessage封装一个自己的HttpInputMessage
クラスXHttpInputMessage実装HttpInputMessage {
    プライベートHttpHeadersヘッダー。
    民間のInputStream本体と、
 
    公共XHttpInputMessage(HttpInputMessage httpInputMessage、文字エンコード)がIOExceptionが{スロー
        this.headers = httpInputMessage.getHeaders()。
        this.body =エンコード(httpInputMessage.getBody()、エンコード)。
    }
 
    プライベートのInputStreamエンコード(入力ストリーム本体、文字エンコード){
        //省略对流进行编码的操作
        戻り体;
    }
 
    @Override
    公共入力ストリームgetBody(){
        戻り体;
    }
 
    @Override
    HttpHeaders getHeadersパブリック(){
        戻りNULL;
    }
}
スプリングデフォルト抽象実装クラスRequestBodyAdviceAdapterインタフェースを提供し、我々は、コードがもう少し簡潔にするために、オンデマンドで実現するために、このクラスを拡張することができ

パッケージorg.springframework.web.servlet.mvc.method.annotation。
 
インポートにjava.io.IOException;
輸入java.lang.reflect.Type;
 
輸入org.springframework.core.MethodParameter;
輸入org.springframework.http.HttpInputMessage;
輸入org.springframework.http.converter.HttpMessageConverter;
輸入org.springframework.lang.Nullable;
 
パブリック抽象クラスRequestBodyAdviceAdapter実装RequestBodyAdvice {
 
    @Override
    公共HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage、MethodParameterパラメータ、
            ??タイプたtargetType、クラスは<拡張HttpMessageConverter <>> converterType)は
            IOExceptionが{スロー
 
        リターンinputMessageを。
    }
 
 
    @Override
    (オブジェクト本体、HttpInputMessage inputMessage、MethodParameterパラメータ、パブリックオブジェクトafterBodyRead
            ?タイプたtargetType、クラス<延びHttpMessageConverter <>> converterType){
 
        戻り体;
    }
 
 
    @Override
    @Nullable
    パブリックオブジェクトhandleEmptyBody(@Nullableオブジェクト本体、HttpInputMessage inputMessage、
            MethodParameterパラメータ、タイプたtargetType、
            クラス<延びHttpMessageConverter <>> converterType?){
 
        戻り体;
    }
}
 

 

最後に、上で掲示される:2019年8月2日12時33分23秒
0人が報酬
助けを求める民間の手紙は
RequestBodyAdviceとResponseBodyAdviceリターンJSONインターフェースの暗号化と復号化の継承された
読取回数774

RequestBodyAdviceはそのインターフェースは、暗号化と復号化を必要とする場合、操作が@ResponseBodyした後に行うようResponseBodyAdviceを理解することができ、作る@RequestBody必要前の操作として理解することができます...
ボーエンは
から:u013780371のブログ


----------------
免責事項:この記事はCSDNブロガー元の記事「BlueKitty1210」で、CC 4.0 BY-SAの著作権契約書に従ってください、複製、元のソースのリンクと、この文を添付してください。 。
オリジナルリンクします。https://blog.csdn.net/xingbaozhen1210/article/details/98189562

公開された51元の記事 ウォン称賛80 ビュー930 000 +

おすすめ

転載: blog.csdn.net/xiyang_1990/article/details/103092906