编辑文章 公众号爬虫 null <p></p> <div id="js_article" class="rich_media"> <div id="js_top_ad_area" class="top_banner"> </div> <div class="rich_media_inner"> <div id="page-content" class="rich_media_area_primary"> <div class="rich_media_area_primary_inner"> <div id="js_content" class="rich_media_content js_underline_content autoTypeSetting24psection " style="visibility: show;"> <section style="background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.75; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; overflow-wrap: break-word; text-align: left; padding: 0px 10px 0px 10px; margin: 0px;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" data-pm-slice="0 0 []"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">说到 Controller,相信大家都不陌生,它可以很方便地对外提供数据接口。它的定位,我认为是<code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">不可或缺的配角</code>,说它不可或缺是因为无论是传统的三层架构还是现在的COLA架构,Controller 层依旧有一席之地,说明他的必要性;说它是配角是因为 Controller 层的代码一般是不负责具体的逻辑业务逻辑实现,但是它负责接收和响应请求。</p> <h3 style="display: table; text-align: center; line-height: 1.2; font-size: 17.6px; border-bottom-width: 4px; border-bottom-style: solid; border-bottom-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; color: #0a0a0a; font-weight: bold; padding: 4px 8px 4px 8px; margin: 2em auto 1em auto;" data-tool="mdnice编辑器"><span style="font-size: 20px; color: #0a0a0a; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">一、从现状看问题</span></h3> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">Controller 主要的工作有以下几项:</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">接收请求并解析参数</section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">调用 Service 执行具体的业务代码(可能包含参数校验)</section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">捕获业务逻辑异常做出反馈</section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">业务逻辑执行成功做出响应</section> </li> </ul> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">//DTO</span><br /><span style="color: #61aeee; line-height: 26px;">@Data</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestDTO</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">private</span> Integer num;<br /> <span style="color: #c678dd; line-height: 26px;">private</span> String type;<br />}<br /><br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//Service</span><br /><span style="color: #61aeee; line-height: 26px;">@Service</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestService</span> </span>{<br /><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Double <span style="color: #61aeee; line-height: 26px;">service</span><span style="line-height: 26px;">(TestDTO testDTO)</span> <span style="color: #c678dd; line-height: 26px;">throws</span> Exception </span>{<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (testDTO.getNum() <= <span style="color: #d19a66; line-height: 26px;">0</span>) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> Exception(<span style="color: #98c379; line-height: 26px;">"输入的数字需要大于0"</span>);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (testDTO.getType().equals(<span style="color: #98c379; line-height: 26px;">"square"</span>)) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Math.pow(testDTO.getNum(), <span style="color: #d19a66; line-height: 26px;">2</span>);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (testDTO.getType().equals(<span style="color: #98c379; line-height: 26px;">"factorial"</span>)) {<br /> <span style="color: #c678dd; line-height: 26px;">double</span> result = <span style="color: #d19a66; line-height: 26px;">1</span>;<br /> <span style="color: #c678dd; line-height: 26px;">int</span> num = testDTO.getNum();<br /> <span style="color: #c678dd; line-height: 26px;">while</span> (num > <span style="color: #d19a66; line-height: 26px;">1</span>) {<br /> result = result * num;<br /> num -= <span style="color: #d19a66; line-height: 26px;">1</span>;<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> result;<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> Exception(<span style="color: #98c379; line-height: 26px;">"未识别的算法"</span>);<br /> }<br />}<br /><br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//Controller</span><br /><span style="color: #61aeee; line-height: 26px;">@RestController</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestController</span> </span>{<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span> TestService testService;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@PostMapping</span>(<span style="color: #98c379; line-height: 26px;">"/test"</span>)<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Double <span style="color: #61aeee; line-height: 26px;">test</span><span style="line-height: 26px;">(@RequestBody TestDTO testDTO)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">try</span> {<br /> Double result = <span style="color: #c678dd; line-height: 26px;">this</span>.testService.service(testDTO);<br /> <span style="color: #c678dd; line-height: 26px;">return</span> result;<br /> } <span style="color: #c678dd; line-height: 26px;">catch</span> (Exception e) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> RuntimeException(e);<br /> }<br /> }<br /><br /> <span style="color: #61aeee; line-height: 26px;">@Autowired</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> DTOid <span style="color: #61aeee; line-height: 26px;">setTestService</span><span style="line-height: 26px;">(TestService testService)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">this</span>.testService = testService;<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">如果真的按照上面所列的工作项来开发 Controller 代码会有几个问题:</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">参数校验过多地耦合了业务代码,违背单一职责原则</section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">可能在多个业务中都抛出同一个异常,导致代码重复</section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">各种异常反馈和成功响应格式不统一,接口对接不友好</section> </li> </ul> <h3 style="display: table; text-align: center; line-height: 1.2; font-size: 17.6px; border-bottom-width: 4px; border-bottom-style: solid; border-bottom-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; color: #0a0a0a; font-weight: bold; padding: 4px 8px 4px 8px; margin: 2em auto 1em auto;" data-tool="mdnice编辑器"><span style="font-size: 20px; color: #0a0a0a; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">二、改造 Controller 层逻辑</span></h3> <h4 style="display: block; text-align: left; line-height: 1.1; font-size: 17px; border-left-width: 4px; border-left-style: solid; border-left-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-weight: bold; padding: 0px 0px 0px 8px; margin: 2em 0px 0.5em 0px;" data-tool="mdnice编辑器"><span style="font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">统一返回结构</span></h4> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">统一返回值类型无论项目前后端是否分离都是非常必要的,方便对接接口的开发人员更加清晰地知道这个接口的调用是否成功(不能仅仅简单地看返回值是否为 null 就判断成功与否,因为有些接口的设计就是如此),使用一个状态码、状态信息就能清楚地了解接口调用情况。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">//定义返回数据结构</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">interface</span> <span style="color: #e6c07b; line-height: 26px;">IResult</span> </span>{<br /> <span style="line-height: 26px;">Integer <span style="color: #61aeee; line-height: 26px;">getCode</span><span style="line-height: 26px;">()</span></span>;<br /> <span style="line-height: 26px;">String <span style="color: #61aeee; line-height: 26px;">getMessage</span><span style="line-height: 26px;">()</span></span>;<br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//常用结果的枚举</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">enum</span> ResultEnum implements IResult {<br /> SUCCESS(<span style="color: #d19a66; line-height: 26px;">2001</span>, <span style="color: #98c379; line-height: 26px;">"接口调用成功"</span>),<br /> VALIDATE_FAILED(<span style="color: #d19a66; line-height: 26px;">2002</span>, <span style="color: #98c379; line-height: 26px;">"参数校验失败"</span>),<br /> COMMON_FAILED(<span style="color: #d19a66; line-height: 26px;">2003</span>, <span style="color: #98c379; line-height: 26px;">"接口调用失败"</span>),<br /> FORBIDDEN(<span style="color: #d19a66; line-height: 26px;">2004</span>, <span style="color: #98c379; line-height: 26px;">"没有权限访问资源"</span>);<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span> Integer code;<br /> <span style="color: #c678dd; line-height: 26px;">private</span> String message;<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//省略get、set方法和构造方法</span><br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//统一返回数据结构</span><br /><span style="color: #61aeee; line-height: 26px;">@Data</span><br /><span style="color: #61aeee; line-height: 26px;">@NoArgsConstructor</span><br /><span style="color: #61aeee; line-height: 26px;">@AllArgsConstructor</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><<span style="color: #e6c07b; line-height: 26px;">T</span>> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">private</span> Integer code;<br /> <span style="color: #c678dd; line-height: 26px;">private</span> String message;<br /> <span style="color: #c678dd; line-height: 26px;">private</span> T data;<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> <T> <span style="line-height: 26px;">Result<T> <span style="color: #61aeee; line-height: 26px;">success</span><span style="line-height: 26px;">(T data)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">new</span> Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> <T> <span style="line-height: 26px;">Result<T> <span style="color: #61aeee; line-height: 26px;">success</span><span style="line-height: 26px;">(String message, T data)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">new</span> Result<>(ResultEnum.SUCCESS.getCode(), message, data);<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> Result<?> failed() {<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">new</span> Result<>(ResultEnum.COMMON_FAILED.getCode(), ResultEnum.COMMON_FAILED.getMessage(), <span style="color: #c678dd; line-height: 26px;">null</span>);<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> Result<?> failed(String message) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">new</span> Result<>(ResultEnum.COMMON_FAILED.getCode(), message, <span style="color: #c678dd; line-height: 26px;">null</span>);<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> Result<?> failed(IResult errorResult) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">new</span> Result<>(errorResult.getCode(), errorResult.getMessage(), <span style="color: #c678dd; line-height: 26px;">null</span>);<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">static</span> <T> <span style="line-height: 26px;">Result<T> <span style="color: #61aeee; line-height: 26px;">instance</span><span style="line-height: 26px;">(Integer code, String message, T data)</span> </span>{<br /> Result<T> result = <span style="color: #c678dd; line-height: 26px;">new</span> Result<>();<br /> result.setCode(code);<br /> result.setMessage(message);<br /> result.setData(data);<br /> <span style="color: #c678dd; line-height: 26px;">return</span> result;<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">统一返回结构后,在 Controller 中就可以使用了,但是每一个 Controller 都写这么一段最终封装的逻辑,这些都是很重复的工作,所以还要继续想办法进一步处理统一返回结构。</p> <h4 style="display: block; text-align: left; line-height: 1.1; font-size: 17px; border-left-width: 4px; border-left-style: solid; border-left-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-weight: bold; padding: 0px 0px 0px 8px; margin: 2em 0px 0.5em 0px;" data-tool="mdnice编辑器"><span style="font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">统一包装处理</span></h4> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">Spring 中提供了一个类 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ResponseBodyAdvice</code> ,能帮助我们实现上述需求。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ResponseBodyAdvice</code> 是对 Controller 返回的内容在 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">HttpMessageConverter</code> 进行类型转换之前拦截,进行相应的处理操作后,再将结果返回给客户端。那这样就可以把统一包装的工作放到这个类里面。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">interface</span> <span style="color: #e6c07b; line-height: 26px;">ResponseBodyAdvice</span><<span style="color: #e6c07b; line-height: 26px;">T</span>> </span>{<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">boolean</span> <span style="color: #61aeee; line-height: 26px;">supports</span><span style="line-height: 26px;">(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType)</span></span>;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@Nullable</span><br /> <span style="line-height: 26px;">T <span style="color: #61aeee; line-height: 26px;">beforeBodyWrite</span><span style="line-height: 26px;">(@Nullable T body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response)</span></span>;<br />}<br /></code></pre> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">supports:</strong> 判断是否要交给 beforeBodyWrite 方法执行,ture:需要;false:不需要</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">beforeBodyWrite:</strong> 对 response 进行具体的处理。</p> </section> </li> </ul> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">// 如果引入了swagger或knife4j的文档生成组件,这里需要仅扫描自己项目的包,否则文档无法正常生成</span><br /><span style="color: #61aeee; line-height: 26px;">@RestControllerAdvice</span>(basePackages = <span style="color: #98c379; line-height: 26px;">"com.example.demo"</span>)<br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">ResponseAdvice</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">ResponseBodyAdvice</span><<span style="color: #e6c07b; line-height: 26px;">Object</span>> </span>{<br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">boolean</span> <span style="color: #61aeee; line-height: 26px;">supports</span><span style="line-height: 26px;">(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType)</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 如果不需要进行封装的,可以添加一些校验手段,比如添加标记排除的注解</span><br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">true</span>;<br /> }<br /><br /><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Object <span style="color: #61aeee; line-height: 26px;">beforeBodyWrite</span><span style="line-height: 26px;">(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response)</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 提供一定的灵活度,如果body已经被包装了,就不进行包装</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (body <span style="color: #c678dd; line-height: 26px;">instanceof</span> Result) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> body;<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.success(body);<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">经过这样改造,既能实现对 Controller 返回的数据进行统一包装,又不需要对原有代码进行大量的改动。</p> <h4 style="display: block; text-align: left; line-height: 1.1; font-size: 17px; border-left-width: 4px; border-left-style: solid; border-left-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-weight: bold; padding: 0px 0px 0px 8px; margin: 2em 0px 0.5em 0px;" data-tool="mdnice编辑器"><span style="font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">处理 <code>cannot be cast to java.lang.String</code> 问题</span></h4> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">如果直接使用 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ResponseBodyAdvice</code>,对于一般的类型都没有问题,当处理字符串类型时,会抛出 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">xxx.包装类 cannot be cast to java.lang.String</code> 的类型转换的异常。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">在 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ResponseBodyAdvice</code> 实现类中 debug 发现,只有 String 类型的 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">selectedConverterType</code> 参数值是 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">org.springframework.http.converter.StringHttpMessageConverter</code>,而其他数据类型的值是<code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">org.springframework.http.converter.json.MappingJackson2HttpMessageConverter</code>。</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">String 类型<img src="http://img.liflag.cn/post/46a8962294e1ce1f2b71d51c09161c44.png" /></section> </li> </ul> <figure style="display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"></figure> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;">其他类型 (如 Integer 类型)<img src="http://img.liflag.cn/post/386759581d7479e57aeba3c3fb199900.png" /></section> </li> </ul> <figure style="display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"></figure> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">现在问题已经较为清晰了,因为我们需要返回一个 Result 对象。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">所以使用 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MappingJackson2HttpMessageConverter</code> 是可以正常转换的。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">而使用 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">StringHttpMessageConverter</code> 字符串转换器会导致类型转换失败。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">现在处理这个问题有两种方式:</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">①在 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">beforeBodyWrite</code> 方法处进行判断,如果返回值是 String 类型就对 Result 对象手动进行转换成 JSON 字符串,另外方便前端使用,最好在 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestMapping</code> 中指定 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ContentType</code>。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #61aeee; line-height: 26px;">@RestControllerAdvice</span>(basePackages = <span style="color: #98c379; line-height: 26px;">"com.example.demo"</span>)<br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">ResponseAdvice</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">ResponseBodyAdvice</span><<span style="color: #e6c07b; line-height: 26px;">Object</span>> </span>{<br /> ...<br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Object <span style="color: #61aeee; line-height: 26px;">beforeBodyWrite</span><span style="line-height: 26px;">(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response)</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 提供一定的灵活度,如果body已经被包装了,就不进行包装</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (body <span style="color: #c678dd; line-height: 26px;">instanceof</span> Result) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> body;<br /> }<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 如果返回值是String类型,那就手动把Result对象转换成JSON字符串</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (body <span style="color: #c678dd; line-height: 26px;">instanceof</span> String) {<br /> <span style="color: #c678dd; line-height: 26px;">try</span> {<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">this</span>.objectMapper.writeValueAsString(Result.success(body));<br /> } <span style="color: #c678dd; line-height: 26px;">catch</span> (JsonProcessingException e) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> RuntimeException(e);<br /> }<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.success(body);<br /> }<br /> ...<br />}<br /><br /><span style="color: #61aeee; line-height: 26px;">@GetMapping</span>(value = <span style="color: #98c379; line-height: 26px;">"/returnString"</span>, produces = <span style="color: #98c379; line-height: 26px;">"application/json; charset=UTF-8"</span>)<br /><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> String <span style="color: #61aeee; line-height: 26px;">returnString</span><span style="line-height: 26px;">()</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #98c379; line-height: 26px;">"success"</span>;<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">②修改 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">HttpMessageConverter</code> 实例集合中 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MappingJackson2HttpMessageConverter</code> 的顺序。因为发生上述问题的根源所在是集合中 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">StringHttpMessageConverter</code> 的顺序先于 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MappingJackson2HttpMessageConverter</code> 的,调整顺序后即可从根源上解决这个问题。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">网上有不少做法是直接在集合中第一位添加 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MappingJackson2HttpMessageConverter</code>。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #61aeee; line-height: 26px;">@Configuration</span><br /><span style="color: #c678dd; line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">WebConfiguration</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">WebMvcConfigurer</span> </span>{<br /> <br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">configureMessageConverters</span><span style="line-height: 26px;">(List<HttpMessageConverter<?>> converters)</span> </span>{<br /> converters.add(<span style="color: #d19a66; line-height: 26px;">0</span>, <span style="color: #c678dd; line-height: 26px;">new</span> MappingJackson2HttpMessageConverter());<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">诚然,这种方式可以解决问题,但其实问题的根源不是集合中缺少这一个转换器,而是转换器的顺序导致的,所以最合理的做法应该是调整 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MappingJackson2HttpMessageConverter</code> 在集合中的顺序。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #61aeee; line-height: 26px;">@Configuration</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">WebMvcConfiguration</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">WebMvcConfigurer</span> </span>{<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 交换MappingJackson2HttpMessageConverter与第一位元素<br /> * 让返回值类型为String的接口能正常返回包装结果<br /> *<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> converters initially an empty list of converters<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">configureMessageConverters</span><span style="line-height: 26px;">(List<HttpMessageConverter<?>> converters)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">for</span> (<span style="color: #c678dd; line-height: 26px;">int</span> i = <span style="color: #d19a66; line-height: 26px;">0</span>; i < converters.size(); i++) {<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (converters.get(i) <span style="color: #c678dd; line-height: 26px;">instanceof</span> MappingJackson2HttpMessageConverter) {<br /> MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converters.get(i);<br /> converters.set(i, converters.get(<span style="color: #d19a66; line-height: 26px;">0</span>));<br /> converters.set(<span style="color: #d19a66; line-height: 26px;">0</span>, mappingJackson2HttpMessageConverter);<br /> <span style="color: #c678dd; line-height: 26px;">break</span>;<br /> }<br /> }<br /> }<br />}<br /></code></pre> <h4 style="display: block; text-align: left; line-height: 1.1; font-size: 17px; border-left-width: 4px; border-left-style: solid; border-left-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-weight: bold; padding: 0px 0px 0px 8px; margin: 2em 0px 0.5em 0px;" data-tool="mdnice编辑器"><span style="font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">参数校验</span></h4> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">Java API 的规范 JSR303 定义了校验的标准 validation-api ,其中一个比较出名的实现是 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">hibernate validation</code> ,<code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">spring validation</code> 是对其的二次封装,常用于 SpringMVC 的参数自动校验,参数校验的代码就不需要再与业务逻辑代码进行耦合了。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">@PathVariable 和 @RequestParam 参数校验</strong></p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">Get 请求的参数接收一般依赖这两个注解,但是处于 url 有长度限制和代码的可维护性,超过 5 个参数尽量用实体来传参。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">对 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@PathVariable</code> 和 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestParam</code> 参数进行校验需要在入参声明约束的注解。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">如果校验失败,会抛出 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MethodArgumentNotValidException</code> 异常。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #61aeee; line-height: 26px;">@RestController</span>(value = <span style="color: #98c379; line-height: 26px;">"prettyTestController"</span>)<br /><span style="color: #61aeee; line-height: 26px;">@RequestMapping</span>(<span style="color: #98c379; line-height: 26px;">"/pretty"</span>)<br /><span style="color: #61aeee; line-height: 26px;">@Validated</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestController</span> </span>{<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span> TestService testService;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@GetMapping</span>(<span style="color: #98c379; line-height: 26px;">"/{num}"</span>)<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Integer <span style="color: #61aeee; line-height: 26px;">detail</span><span style="line-height: 26px;">(@PathVariable(<span style="color: #98c379; line-height: 26px;">"num"</span>)</span> @<span style="color: #61aeee; line-height: 26px;">Min</span><span style="line-height: 26px;">(<span style="color: #d19a66; line-height: 26px;">1</span>)</span> @<span style="color: #61aeee; line-height: 26px;">Max</span><span style="line-height: 26px;">(<span style="color: #d19a66; line-height: 26px;">20</span>)</span> Integer num) </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span> num * num;<br /> }<br /><br /> <span style="color: #61aeee; line-height: 26px;">@GetMapping</span>(<span style="color: #98c379; line-height: 26px;">"/getByEmail"</span>)<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> TestDTO <span style="color: #61aeee; line-height: 26px;">getByAccount</span><span style="line-height: 26px;">(@RequestParam @NotBlank @Email String email)</span> </span>{<br /> TestDTO testDTO = <span style="color: #c678dd; line-height: 26px;">new</span> TestDTO();<br /> testDTO.setEmail(email);<br /> <span style="color: #c678dd; line-height: 26px;">return</span> testDTO;<br /> }<br /><br /> <span style="color: #61aeee; line-height: 26px;">@Autowired</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">setTestService</span><span style="line-height: 26px;">(TestService prettyTestService)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">this</span>.testService = prettyTestService;<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">校验原理</strong></p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">在 SpringMVC 中,有一个类是 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">RequestResponseBodyMethodProcessor</code> ,这个类有两个作用(实际上可以从名字上得到一点启发)。</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">用于解析 <code style="height: auto; line-height: 1.8em; letter-spacing: 0em; width: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestBody</code> 标注的参数</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">处理 <code style="height: auto; line-height: 1.8em; letter-spacing: 0em; width: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@ResponseBody</code> 标注方法的返回值</p> </section> </li> </ul> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">解析 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestBoyd</code> 标注参数的方法是 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">resolveArgument</code>。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">RequestResponseBodyMethodProcessor</span> <span style="color: #c678dd; line-height: 26px;">extends</span> <span style="color: #e6c07b; line-height: 26px;">AbstractMessageConverterMethodProcessor</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * Throws MethodArgumentNotValidException if validation fails.<br /> * <span style="color: #c678dd; line-height: 26px;">@throws</span> HttpMessageNotReadableException if {<span style="color: #c678dd; line-height: 26px;">@link</span> RequestBody#required()}<br /> * is {<span style="color: #c678dd; line-height: 26px;">@code</span> true} and there is no body content or if there is no suitable<br /> * converter to read the content with.<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Object <span style="color: #61aeee; line-height: 26px;">resolveArgument</span><span style="line-height: 26px;">(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,<br /> NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory)</span> <span style="color: #c678dd; line-height: 26px;">throws</span> Exception </span>{<br /><br /> parameter = parameter.nestedIfOptional();<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//把请求数据封装成标注的DTO对象</span><br /> Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());<br /> String name = Conventions.getVariableNameForParameter(parameter);<br /><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (binderFactory != <span style="color: #c678dd; line-height: 26px;">null</span>) {<br /> WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (arg != <span style="color: #c678dd; line-height: 26px;">null</span>) {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//执行数据校验</span><br /> validateIfApplicable(binder, parameter);<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//如果校验不通过,就抛出MethodArgumentNotValidException异常</span><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//如果我们不自己捕获,那么最终会由DefaultHandlerExceptionResolver捕获处理</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> MethodArgumentNotValidException(parameter, binder.getBindingResult());<br /> }<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (mavContainer != <span style="color: #c678dd; line-height: 26px;">null</span>) {<br /> mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());<br /> }<br /> }<br /><br /> <span style="color: #c678dd; line-height: 26px;">return</span> adaptArgumentIfNecessary(arg, parameter);<br /> }<br />}<br /><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="color: #c678dd; line-height: 26px;">abstract</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">AbstractMessageConverterMethodArgumentResolver</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">HandlerMethodArgumentResolver</span> </span>{<br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * Validate the binding target if applicable.<br /> * <p>The default implementation checks for {<span style="color: #c678dd; line-height: 26px;">@code</span> <span style="color: #c678dd; line-height: 26px;">@javax</span>.validation.Valid},<br /> * Spring's {<span style="color: #c678dd; line-height: 26px;">@link</span> org.springframework.validation.annotation.Validated},<br /> * and custom annotations whose name starts with "Valid".<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> binder the DataBinder to be used<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> parameter the method parameter descriptor<br /> * <span style="color: #c678dd; line-height: 26px;">@since</span> 4.1.5<br /> * <span style="color: #c678dd; line-height: 26px;">@see</span> #isBindExceptionRequired<br /> */</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">protected</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">validateIfApplicable</span><span style="line-height: 26px;">(WebDataBinder binder, MethodParameter parameter)</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//获取参数上的所有注解</span><br /> Annotation[] annotations = parameter.getParameterAnnotations();<br /> <span style="color: #c678dd; line-height: 26px;">for</span> (Annotation ann : annotations) {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//如果注解中包含了@Valid、@Validated或者是名字以Valid开头的注解就进行参数校验</span><br /> Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann);<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (validationHints != <span style="color: #c678dd; line-height: 26px;">null</span>) {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//实际校验逻辑,最终会调用Hibernate Validator执行真正的校验</span><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//所以Spring Validation是对Hibernate Validation的二次封装</span><br /> binder.validate(validationHints);<br /> <span style="color: #c678dd; line-height: 26px;">break</span>;<br /> }<br /> }<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">@RequestBody 参数校验</strong></p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">Post、Put 请求的参数推荐使用 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestBody</code> 请求体参数。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">对 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@RequestBody</code> 参数进行校验需要在 DTO 对象中加入校验条件后,再搭配 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">@Validated</code> 即可完成自动校验。</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">如果校验失败,会抛出 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">ConstraintViolationException</code> 异常。</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">//DTO</span><br /><span style="color: #61aeee; line-height: 26px;">@Data</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestDTO</span> </span>{<br /> <span style="color: #61aeee; line-height: 26px;">@NotBlank</span><br /> <span style="color: #c678dd; line-height: 26px;">private</span> String userName;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@NotBlank</span><br /> <span style="color: #61aeee; line-height: 26px;">@Length</span>(min = <span style="color: #d19a66; line-height: 26px;">6</span>, max = <span style="color: #d19a66; line-height: 26px;">20</span>)<br /> <span style="color: #c678dd; line-height: 26px;">private</span> String password;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@NotNull</span><br /> <span style="color: #61aeee; line-height: 26px;">@Email</span><br /> <span style="color: #c678dd; line-height: 26px;">private</span> String email;<br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//Controller</span><br /><span style="color: #61aeee; line-height: 26px;">@RestController</span>(value = <span style="color: #98c379; line-height: 26px;">"prettyTestController"</span>)<br /><span style="color: #61aeee; line-height: 26px;">@RequestMapping</span>(<span style="color: #98c379; line-height: 26px;">"/pretty"</span>)<br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">TestController</span> </span>{<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span> TestService testService;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@PostMapping</span>(<span style="color: #98c379; line-height: 26px;">"/test-validation"</span>)<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">testValidation</span><span style="line-height: 26px;">(@RequestBody @Validated TestDTO testDTO)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">this</span>.testService.save(testDTO);<br /> }<br /><br /> <span style="color: #61aeee; line-height: 26px;">@Autowired</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">setTestService</span><span style="line-height: 26px;">(TestService testService)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">this</span>.testService = testService;<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">校验原理</strong></p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">声明约束的方式,注解加到了参数上面,可以比较容易猜测到是使用了 AOP 对方法进行增强</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">而实际上 Spring 也是通过 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MethodValidationPostProcessor</code> 动态注册 AOP 切面,然后使用 <code style="line-height: 1.8em; letter-spacing: 0em; width: auto; height: auto; overflow-wrap: break-word; font-family: Consolas, Monaco, Menlo, monospace; word-break: break-all; font-size: 90%; color: #dd1144; background-image: initial; background-position-x: initial; background-position-y: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgba(27, 31, 35, 0.05); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; border-width: 3px; border-color: #000000 rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4) rgba(0, 0, 0, 0.4); border-style: none; padding: 3px 5px 3px 5px; margin: 0px 2px 0px 2px;">MethodValidationInterceptor</code> 对切点方法进行织入增强</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">MethodValidationPostProcessor</span> <span style="color: #c678dd; line-height: 26px;">extends</span> <span style="color: #e6c07b; line-height: 26px;">AbstractBeanFactoryAwareAdvisingPostProcessor</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">InitializingBean</span> </span>{<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//指定了创建切面的Bean的注解</span><br /> <span style="color: #c678dd; line-height: 26px;">private</span> Class<? extends Annotation> validatedAnnotationType = Validated<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span></span>;<br /><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">afterPropertiesSet</span><span style="line-height: 26px;">()</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//为所有@Validated标注的Bean创建切面</span><br /> Pointcut pointcut = <span style="color: #c678dd; line-height: 26px;">new</span> AnnotationMatchingPointcut(<span style="color: #c678dd; line-height: 26px;">this</span>.validatedAnnotationType, <span style="color: #c678dd; line-height: 26px;">true</span>);<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//创建Advisor进行增强</span><br /> <span style="color: #c678dd; line-height: 26px;">this</span>.advisor = <span style="color: #c678dd; line-height: 26px;">new</span> DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(<span style="color: #c678dd; line-height: 26px;">this</span>.validator));<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//创建Advice,本质就是一个方法拦截器</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">protected</span> Advice <span style="color: #61aeee; line-height: 26px;">createMethodValidationAdvice</span><span style="line-height: 26px;">(@Nullable Validator validator)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span> (validator != <span style="color: #c678dd; line-height: 26px;">null</span> ? <span style="color: #c678dd; line-height: 26px;">new</span> MethodValidationInterceptor(validator) : <span style="color: #c678dd; line-height: 26px;">new</span> MethodValidationInterceptor());<br /> }<br />}<br /><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">MethodValidationInterceptor</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">MethodInterceptor</span> </span>{<br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> Object <span style="color: #61aeee; line-height: 26px;">invoke</span><span style="line-height: 26px;">(MethodInvocation invocation)</span> <span style="color: #c678dd; line-height: 26px;">throws</span> Throwable </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//无需增强的方法,直接跳过</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (isFactoryBeanMetadataMethod(invocation.getMethod())) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> invocation.proceed();<br /> }<br /> <br /> Class<?>[] groups = determineValidationGroups(invocation);<br /> ExecutableValidator execVal = <span style="color: #c678dd; line-height: 26px;">this</span>.validator.forExecutables();<br /> Method methodToValidate = invocation.getMethod();<br /> Set<ConstraintViolation<Object>> result;<br /> <span style="color: #c678dd; line-height: 26px;">try</span> {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//方法入参校验,最终还是委托给Hibernate Validator来校验</span><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//所以Spring Validation是对Hibernate Validation的二次封装</span><br /> result = execVal.validateParameters(<br /> invocation.getThis(), methodToValidate, invocation.getArguments(), groups);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">catch</span> (IllegalArgumentException ex) {<br /> ...<br /> }<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//校验不通过抛出ConstraintViolationException异常</span><br /> <span style="color: #c678dd; line-height: 26px;">if</span> (!result.isEmpty()) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> ConstraintViolationException(result);<br /> }<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//Controller方法调用</span><br /> Object returnValue = invocation.proceed();<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">//下面是对返回值做校验,流程和上面大概一样</span><br /> result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (!result.isEmpty()) {<br /> <span style="color: #c678dd; line-height: 26px;">throw</span><span style="color: #c678dd; line-height: 26px;">new</span> ConstraintViolationException(result);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> returnValue;<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器"><strong style="color: #0f4c81; font-weight: bold; background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; height: auto; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 3px; border-color: rgba(0, 0, 0, 0.4); border-style: none; padding: 0px; margin: 0px;">自定义校验规则</strong></p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">有些时候 JSR303 标准中提供的校验规则不满足复杂的业务需求,也可以自定义校验规则</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">自定义校验规则需要做两件事情</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">自定义注解类,定义错误信息和一些其他需要的内容</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">注解校验器,定义判定规则</p> </section> </li> </ul> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">//自定义注解类</span><br /><span style="color: #61aeee; line-height: 26px;">@Target</span>({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})<br /><span style="color: #61aeee; line-height: 26px;">@Retention</span>(RetentionPolicy.RUNTIME)<br /><span style="color: #61aeee; line-height: 26px;">@Documented</span><br /><span style="color: #61aeee; line-height: 26px;">@Constraint</span>(validatedBy = MobileValidator<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>)<br /><span style="color: #e6c07b; line-height: 26px;">public</span> @<span style="color: #e6c07b; line-height: 26px;">interface</span> <span style="color: #e6c07b; line-height: 26px;">Mobile</span> </span>{<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 是否允许为空<br /> */</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">boolean</span> <span style="color: #61aeee; line-height: 26px;">required</span><span style="line-height: 26px;">()</span> <span style="color: #c678dd; line-height: 26px;">default</span> <span style="color: #c678dd; line-height: 26px;">true</span></span>;<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 校验不通过返回的提示信息<br /> */</span><br /> <span style="line-height: 26px;">String <span style="color: #61aeee; line-height: 26px;">message</span><span style="line-height: 26px;">()</span> <span style="color: #c678dd; line-height: 26px;">default</span> "不是一个手机号码格式"</span>;<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * Constraint要求的属性,用于分组校验和扩展,留空就好<br /> */</span><br /> Class<?>[] groups() <span style="color: #c678dd; line-height: 26px;">default</span> {};<br /> Class<? extends Payload>[] payload() <span style="color: #c678dd; line-height: 26px;">default</span> {};<br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//注解校验器</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">MobileValidator</span> <span style="color: #c678dd; line-height: 26px;">implements</span> <span style="color: #e6c07b; line-height: 26px;">ConstraintValidator</span><<span style="color: #e6c07b; line-height: 26px;">Mobile</span>, <span style="color: #e6c07b; line-height: 26px;">CharSequence</span>> </span>{<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span><span style="color: #c678dd; line-height: 26px;">boolean</span> required = <span style="color: #c678dd; line-height: 26px;">false</span>;<br /><br /> <span style="color: #c678dd; line-height: 26px;">private</span><span style="color: #c678dd; line-height: 26px;">final</span> Pattern pattern = Pattern.compile(<span style="color: #98c379; line-height: 26px;">"^1[34578][0-9]{9}$"</span>); <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 验证手机号</span><br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 在验证开始前调用注解里的方法,从而获取到一些注解里的参数<br /> *<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> constraintAnnotation annotation instance for a given constraint declaration<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">void</span> <span style="color: #61aeee; line-height: 26px;">initialize</span><span style="line-height: 26px;">(Mobile constraintAnnotation)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">this</span>.required = constraintAnnotation.required();<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 判断参数是否合法<br /> *<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> value object to validate<br /> * <span style="color: #c678dd; line-height: 26px;">@param</span> context context in which the constraint is evaluated<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@Override</span><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #c678dd; line-height: 26px;">boolean</span> <span style="color: #61aeee; line-height: 26px;">isValid</span><span style="line-height: 26px;">(CharSequence value, ConstraintValidatorContext context)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (<span style="color: #c678dd; line-height: 26px;">this</span>.required) {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 验证</span><br /> <span style="color: #c678dd; line-height: 26px;">return</span> isMobile(value);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (StringUtils.hasText(value)) {<br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">// 验证</span><br /> <span style="color: #c678dd; line-height: 26px;">return</span> isMobile(value);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span><span style="color: #c678dd; line-height: 26px;">true</span>;<br /> }<br /><br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">private</span> <span style="color: #c678dd; line-height: 26px;">boolean</span> <span style="color: #61aeee; line-height: 26px;">isMobile</span><span style="line-height: 26px;">(<span style="color: #c678dd; line-height: 26px;">final</span> CharSequence str)</span> </span>{<br /> Matcher m = pattern.matcher(str);<br /> <span style="color: #c678dd; line-height: 26px;">return</span> m.matches();<br /> }<br />}<br /></code></pre> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">自动校验参数真的是一项非常必要、非常有意义的工作。 JSR303 提供了丰富的参数校验规则,再加上复杂业务的自定义校验规则,完全把参数校验和业务逻辑解耦开,代码更加简洁,符合单一职责原则。</p> <h4 style="display: block; text-align: left; line-height: 1.1; font-size: 17px; border-left-width: 4px; border-left-style: solid; border-left-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-weight: bold; padding: 0px 0px 0px 8px; margin: 2em 0px 0.5em 0px;" data-tool="mdnice编辑器"><span style="font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">自定义异常与统一拦截异常</span></h4> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">原来的代码中可以看到有几个问题</p> <ul class="list-paddingleft-1" style="list-style-type: square; color: #000000; padding: 0px 0px 0px 25px; margin: 8px 0px 8px 0px;"> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">抛出的异常不够具体,只是简单地把错误信息放到了 Exception 中</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">抛出异常后,Controller 不能具体地根据异常做出反馈</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; color: #010101; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; font-weight: normal;"> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;">虽然做了参数自动校验,但是异常返回结构和正常返回结构不一致</p> </section> </li> </ul> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">自定义异常是为了后面统一拦截异常时,对业务中的异常有更加细颗粒度的区分,拦截时针对不同的异常作出不同的响应 而统一拦截异常的目的一个是为了可以与前面定义下来的统一包装返回结构能对应上,另一个是我们希望无论系统发生什么异常,Http 的状态码都要是 200 ,尽可能由业务来区分系统的异常</p> <pre style="border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px; text-align: left; padding: 0px; margin: 10px 0px 10px 0px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; padding-top: 15px; background: #282c34; border-radius: 5px; display: -webkit-box; font-family: Consolas, Monaco, Menlo, monospace; font-size: 12px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">//自定义异常</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">ForbiddenException</span> <span style="color: #c678dd; line-height: 26px;">extends</span> <span style="color: #e6c07b; line-height: 26px;">RuntimeException</span> </span>{<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #61aeee; line-height: 26px;">ForbiddenException</span><span style="line-height: 26px;">(String message)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">super</span>(message);<br /> }<br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//自定义异常</span><br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">BusinessException</span> <span style="color: #c678dd; line-height: 26px;">extends</span> <span style="color: #e6c07b; line-height: 26px;">RuntimeException</span> </span>{<br /> <span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span> <span style="color: #61aeee; line-height: 26px;">BusinessException</span><span style="line-height: 26px;">(String message)</span> </span>{<br /> <span style="color: #c678dd; line-height: 26px;">super</span>(message);<br /> }<br />}<br /><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">//统一拦截异常</span><br /><span style="color: #61aeee; line-height: 26px;">@RestControllerAdvice</span>(basePackages = <span style="color: #98c379; line-height: 26px;">"com.example.demo"</span>)<br /><span style="color: #c678dd; line-height: 26px;">public</span><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span> <span style="color: #e6c07b; line-height: 26px;">ExceptionAdvice</span> </span>{<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 捕获 {<span style="color: #c678dd; line-height: 26px;">@code</span> BusinessException} 异常<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@ExceptionHandler</span>({BusinessException<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>})<br /> <span style="color: #e6c07b; line-height: 26px;">public</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><?> <span style="color: #e6c07b; line-height: 26px;">handleBusinessException</span>(<span style="color: #e6c07b; line-height: 26px;">BusinessException</span> <span style="color: #e6c07b; line-height: 26px;">ex</span>) </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ex.getMessage());<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 捕获 {<span style="color: #c678dd; line-height: 26px;">@code</span> ForbiddenException} 异常<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@ExceptionHandler</span>({ForbiddenException<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>})<br /> <span style="color: #e6c07b; line-height: 26px;">public</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><?> <span style="color: #e6c07b; line-height: 26px;">handleForbiddenException</span>(<span style="color: #e6c07b; line-height: 26px;">ForbiddenException</span> <span style="color: #e6c07b; line-height: 26px;">ex</span>) </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ResultEnum.FORBIDDEN);<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * {<span style="color: #c678dd; line-height: 26px;">@code</span> <span style="color: #c678dd; line-height: 26px;">@RequestBody</span>} 参数校验不通过时抛出的异常处理<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@ExceptionHandler</span>({MethodArgumentNotValidException<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>})<br /> <span style="color: #e6c07b; line-height: 26px;">public</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><?> <span style="color: #e6c07b; line-height: 26px;">handleMethodArgumentNotValidException</span>(<span style="color: #e6c07b; line-height: 26px;">MethodArgumentNotValidException</span> <span style="color: #e6c07b; line-height: 26px;">ex</span>) </span>{<br /> BindingResult bindingResult = ex.getBindingResult();<br /> StringBuilder sb = <span style="color: #c678dd; line-height: 26px;">new</span> StringBuilder(<span style="color: #98c379; line-height: 26px;">"校验失败:"</span>);<br /> <span style="color: #c678dd; line-height: 26px;">for</span> (FieldError fieldError : bindingResult.getFieldErrors()) {<br /> sb.append(fieldError.getField()).append(<span style="color: #98c379; line-height: 26px;">":"</span>).append(fieldError.getDefaultMessage()).append(<span style="color: #98c379; line-height: 26px;">", "</span>);<br /> }<br /> String msg = sb.toString();<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (StringUtils.hasText(msg)) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ResultEnum.VALIDATE_FAILED.getCode(), msg);<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ResultEnum.VALIDATE_FAILED);<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * {<span style="color: #c678dd; line-height: 26px;">@code</span> <span style="color: #c678dd; line-height: 26px;">@PathVariable</span>} 和 {<span style="color: #c678dd; line-height: 26px;">@code</span> <span style="color: #c678dd; line-height: 26px;">@RequestParam</span>} 参数校验不通过时抛出的异常处理<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@ExceptionHandler</span>({ConstraintViolationException<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>})<br /> <span style="color: #e6c07b; line-height: 26px;">public</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><?> <span style="color: #e6c07b; line-height: 26px;">handleConstraintViolationException</span>(<span style="color: #e6c07b; line-height: 26px;">ConstraintViolationException</span> <span style="color: #e6c07b; line-height: 26px;">ex</span>) </span>{<br /> <span style="color: #c678dd; line-height: 26px;">if</span> (StringUtils.hasText(ex.getMessage())) {<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ResultEnum.VALIDATE_FAILED.getCode(), ex.getMessage());<br /> }<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ResultEnum.VALIDATE_FAILED);<br /> }<br /><br /> <span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br /> * 顶级异常捕获并统一处理,当其他异常无法处理时候选择使用<br /> */</span><br /> <span style="color: #61aeee; line-height: 26px;">@ExceptionHandler</span>({Exception<span style="line-height: 26px;">.<span style="color: #c678dd; line-height: 26px;">class</span>})<br /> <span style="color: #e6c07b; line-height: 26px;">public</span> <span style="color: #e6c07b; line-height: 26px;">Result</span><?> <span style="color: #e6c07b; line-height: 26px;">handle</span>(<span style="color: #e6c07b; line-height: 26px;">Exception</span> <span style="color: #e6c07b; line-height: 26px;">ex</span>) </span>{<br /> <span style="color: #c678dd; line-height: 26px;">return</span> Result.failed(ex.getMessage());<br /> }<br /><br />}<br /></code></pre> <h3 style="display: table; text-align: center; line-height: 1.2; font-size: 17.6px; border-bottom-width: 4px; border-bottom-style: solid; border-bottom-color: #0f4c81; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; color: #0a0a0a; font-weight: bold; padding: 4px 8px 4px 8px; margin: 2em auto 1em auto;" data-tool="mdnice编辑器"><span style="font-size: 20px; color: #0a0a0a; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">总结</span></h3> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">做好了这一切改动后,可以发现 Controller 的代码变得非常简洁,可以很清楚地知道每一个参数、每一个 DTO 的校验规则,可以很明确地看到每一个 Controller 方法返回的是什么数据,也可以方便每一个异常应该如何进行反馈</p> <p style="color: #000000; font-size: 16px; line-height: 1.8em; letter-spacing: 0em; text-align: left; text-indent: 0em; padding: 8px 0px 8px 0px; margin: 0px;" data-tool="mdnice编辑器">这一套操作下来后,我们能更加专注于业务逻辑的开发,代码简洁、功能完善,何乐而不为呢?</p> </section> <section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px 10px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0em; -webkit-text-stroke-width: 0px; white-space: normal; background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.75; letter-spacing: 0em; word-break: break-word; text-align: left; visibility: visible;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" data-pm-slice="0 0 []"> <p style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; text-indent: 0px; text-align: center;" data-mpa-action-id="ma15qf5n1198" data-pm-slice="0 0 []"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 13px;" data-mpa-action-id="ma6pkvalhyo" data-pm-slice="0 0 []"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-transform: none; widows: 2; word-spacing: 0em; -webkit-text-stroke-width: 0px; background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; color: #000000; line-height: 1.5em; letter-spacing: 0em; word-break: break-word; visibility: visible; clear: both; min-height: 1em; text-indent: 0px; text-align: center;" data-mpa-action-id="ma6pkva4r4d"><span style="color: #000000;">来</span></span><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-transform: none; widows: 2; word-spacing: 0em; -webkit-text-stroke-width: 0px; background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; color: #000000; line-height: 1.5em; letter-spacing: 0em; word-break: break-word; visibility: visible; clear: both; min-height: 1em; text-indent: 0px; text-align: center;"><span style="color: #000000;">源</span></span><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-transform: none; widows: 2; word-spacing: 0em; -webkit-text-stroke-width: 0px; background: none 0% 0% / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; color: #000000; line-height: 1.5em; letter-spacing: 0em; word-break: break-word; visibility: visible; clear: both; min-height: 1em; text-indent: 0px; text-align: center;"><span style="color: #000000;">:</span></span><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">juejin.cn/post/7123091045071454238</span></span></p> </section> <figure style="-webkit-tap-highlight-color: transparent; margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; font-size: 16px; text-align: left; color: #353535; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; letter-spacing: 0.8px; word-spacing: 0.8px; background-color: #ffffff; display: flex; flex-direction: column; justify-content: center; align-items: center; border-radius: 16px; overflow: show;" data-tool="mdnice编辑器"> <section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px 10px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; word-spacing: 0em; background-image: none; background-position: 0% 0%; background-size: auto; background-repeat: no-repeat; background-attachment: scroll; background-origin: padding-box; background-clip: border-box; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; color: #000000; line-height: 1.5em; letter-spacing: 0em; word-break: break-word;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <p style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 8px 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: #3a3a3a; line-height: 1.8em; letter-spacing: 0em; text-indent: 0em; text-align: right;" data-tool="mdnice编辑器"> </p> </section> </figure> <p style="display: none;"> </p> </div> </div> </div> </div> </div> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: 说到 Controller,相信大家都不陌生,它可以很方便地对外提供数据接口。它的定位,我认为是不可或缺的配角,说它不可或缺是因为无论是传统的三层架构还是现在的COLA架构,Controller 层依旧有一席之地,说明他的... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: B I U H2 H3 引用 • 列表 1. 列表 链接 图片 清格式 源码 该文章是富文本(HTML)写的,已自动切到富文本编辑器,不会按 markdown 解析;要精确改 HTML 点「源码」。 保存修改