{"id":429,"date":"2020-05-14T16:05:24","date_gmt":"2020-05-14T08:05:24","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=429"},"modified":"2020-05-14T16:08:46","modified_gmt":"2020-05-14T08:08:46","slug":"%e9%a1%b9%e7%9b%ae%e9%87%8c%e9%9d%a2interceptor%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=429","title":{"rendered":"\u9879\u76ee\u91cc\u9762Interceptor\u7684\u4f7f\u7528"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">\u5148\u4e0a\u4ee3\u7801<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">StandardOutJsonSaveInterceptor <strong>extends <\/strong>MethodFilterInterceptor<\/pre>\n\n\n\n<p>interceptor\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\uff0c\u8fd9\u91cc\u7ee7\u627fMethodFilterInterceptor\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u9700\u8981\u62e6\u622a\u7684\u65b9\u6cd5\u3002actionInvocation.invoke();\u4e3a\u5b9e\u9645\u7684\u65b9\u6cd5\u8c03\u7528\u8bed\u53e5\u3002\u6839\u636eresponse\u91cc\u9762\u7684\u5185\u5bb9\u4e0d\u540c\uff0c\u53ef\u4ee5\u5904\u7406\u65b9\u6cd5\u7ed3\u675f\u540e\u7684\u4e8b\u9879\uff0c\u6bd4\u5982\u652f\u4ed8\u6210\u529f\u4e86\uff0c\u8981\u901a\u77e5\u53d1\u8d27\u65b9\u3002\u5931\u8d25\u6216\u8005\u5f02\u5e38\uff0c\u5219\u505a\u5176\u4ed6\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Override<br><strong>protected <\/strong>String doIntercept(ActionInvocation actionInvocation){<br>    String result = <strong>null<\/strong>;<br>    <strong>try <\/strong>{<br>        System.<strong><em>out<\/em><\/strong>.println(<strong>\"\u8fdb\u5165StandardOutJsonSaveInterceptor\u62e6\u622a\u5668\"<\/strong>);<br>        <em>\/\/1.\u6267\u884caction\u65b9\u6cd5<br><\/em><em>        <\/em>result = actionInvocation.invoke();<br><br>        <em>\/\/2.\u83b7\u53d6action\u7684response<br><\/em><em>        <\/em>ActionContext actionContext = actionInvocation.getInvocationContext();<br>        Object value = getResponseValue(actionContext);<em>\/\/value\u662faction\u4e2dresponseWrite(json.toString());\u91cc\u9762json.toString()\u7684\u503c<br><\/em><em><br><\/em><em>        \/\/3.\u6839\u636eresponse\u5904\u7406<br><\/em><em>        <\/em>handle(value.toString());<br>    } <strong>catch <\/strong>(Exception e) {<br>        <em>\/\/\u4e0d\u6267\u884c\u53d1\u6d88\u606f\u7684\u64cd\u4f5c<br><\/em><em>        <\/em>e.printStackTrace();<br>    }<br>    <strong>return <\/strong>result;<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * \u83b7\u53d6action\u91cc\u9762\u7684responseWrite\u7684\u503c<br><\/em><em> * <\/em><strong><em>@param <\/em><\/strong><strong><em>actionContext<br><\/em><\/strong><strong><em> <\/em><\/strong><em>* <\/em><strong><em>@return<br><\/em><\/strong><strong><em> <\/em><\/strong><em>* <\/em><strong><em>@throws <\/em><\/strong><em>IOException<br><\/em><em> *\/<br><\/em><strong>private <\/strong>Object getResponseValue(ActionContext actionContext) <strong>throws <\/strong>IOException {<br>    HttpServletResponse response = (HttpServletResponse) actionContext.get(StrutsStatics.<strong><em>HTTP_RESPONSE<\/em><\/strong>);<br>    PrintWriter writer = response.getWriter();<br>    Field ob = ReflectionUtils.<em>findField<\/em>(writer.getClass(),<strong>\"ob\"<\/strong>);<br>    ob.setAccessible(<strong>true<\/strong>);<br>    Object obValue = ReflectionUtils.<em>getField<\/em>(ob, writer);<br>    Field cb = ReflectionUtils.<em>findField<\/em>(obValue.getClass(),<strong>\"cb\"<\/strong>);<br>    cb.setAccessible(<strong>true<\/strong>);<br>    Object value = ReflectionUtils.<em>getField<\/em>(cb, obValue);<br>    <strong>return <\/strong>value;<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> * \u5904\u7406\u53d1\u6d88\u606f\u7b49\u8fc7\u7a0b<br><\/em><em> * <\/em><strong><em>@param <\/em><\/strong><strong><em>result<br><\/em><\/strong><strong><em> <\/em><\/strong><em>*\/<br><\/em><strong>private void <\/strong>handle(String result) {<br>    <em>\/\/\u7b80\u5355\u6253\u5370<br><\/em><em>    <\/em>System.<strong><em>out<\/em><\/strong>.println(<strong>\"response:\"<\/strong>+result);<br>}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5173\u4e8e\u914d\u7f6e<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;<strong>interceptors<\/strong>><br>   <em>&lt;!-- \u5148\u5b9a\u4e49\u62e6\u622a\u5668\u81ea\u5df1\u5199\u7684\u62e6\u622a\u5668\u7c7b name\u968f\u4fbf\u5199 class\u4e3a\u5168\u9650\u5b9a\u540d --><br>       <\/em>&lt;<strong>interceptor name=\"standardOutJsonSaveInterceptor\" class=\"com.yuqiaotech.scm.interceptor.StandardOutJsonSaveInterceptor\"<\/strong>\/><br>       <em>&lt;!-- \u52a0\u5230\u81ea\u5df1\u8bbe\u7f6e\u7684\u62e6\u622a\u5668\u6808\u91cc\u8fb9\u53bb --><br>       <\/em>&lt;<strong>interceptor-stack name=\"myStack\"<\/strong>><br>       <em>&lt;!--   name\u4e3a\u4e0a\u9762interceptor\u6807\u7b7e\u91cc\u9762\u5b9a\u4e49\u7684name--><br>       <\/em>&lt;<strong>interceptor-ref name=\"standardOutJsonSaveInterceptor\"<\/strong>><br>         &lt;<strong>param name=\"includeMethods\"<\/strong>>outJsonSaveStandard&lt;\/<strong>param<\/strong>><br>       &lt;\/<strong>interceptor-ref<\/strong>><br><br>      <em>&lt;!--   \u5728\u81ea\u5df1\u914d\u7f6e\u7684\u62e6\u622a\u5668\u57fa\u7840\u4e0a\uff0c\u5fc5\u987b\u6709struts2\u9ed8\u8ba4\u7684\u62e6\u622a\u5668\uff0c\u4e0d\u7136\u51fa\u9519 --><br>      <\/em>&lt;<strong>interceptor-ref name=\"defaultStack\"<\/strong>>&lt;\/<strong>interceptor-ref<\/strong>><br>       &lt;\/<strong>interceptor-stack<\/strong>><br>&lt;\/<strong>interceptors<\/strong>><br><em>&lt;!-- \u5e94\u7528\u62e6\u622a\u5668\u4e0d\u5199\u62e6\u622a\u5668\u4e0d\u751f\u6548 --><br><\/em>&lt;<strong>default-interceptor-ref name=\"myStack\"<\/strong>\/><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u8981\u653e\u5230struts.xml\u6587\u4ef6\u7684<strong>package\u8282\u70b9\u4e0b\u9762\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u4e0a\u4ee3\u7801 StandardOutJsonSaveInterceptor extends MethodFilte [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[17],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/429"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=429"}],"version-history":[{"count":14,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/429\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}