优美的统一返回结果处理
处理 结果 返回 统一 优美
2023-06-13 09:15:12 时间
优美的统一返回结果处理
我们写项目一般都会自己写一个Result对象,然后去处理,但是有一个问题,逐渐的接口写多了之后
会发现每次都要写这个被Result包裹会很麻烦,这时候我们就可以考虑将他抽取出来,使用一个Handler去处理
package com.zang.blogz.result;
import io.github.vampireachao.stream.core.optional.Op;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author ZVerify
*/
@RestControllerAdvice(basePackages = "com.zang.blogz.controller")
public class AnimeResultHandler implements ResponseBodyAdvice<Object> {
/**
* Whether this component supports the given controller method return type
* and the selected {@code HttpMessageConverter} type.
*
* @param returnType the return type
* @param converterType the selected converter type
* @return {@code true} if {@link #beforeBodyWrite} should be invoked;
* {@code false} otherwise
*/
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return !(returnType.getParameterType().isAssignableFrom(Result.class));
}
/**
* Invoked after an {@code HttpMessageConverter} is selected and just before
* its write method is invoked.
*
* @param body the body to be written
* @param returnType the return type of the controller method
* @param selectedContentType the content type selected through content negotiation
* @param selectedConverterType the converter type selected to write to the response
* @param request the current request
* @param response the current response
* @return the body that was passed in or a modified (possibly new) instance
*/
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
return Op.of(body).map(Result::ok).orElseGet(Result::ok);
}
}
这样只需要在controller层返回要返回的对象就好了,如果想要返回error可以抛出异常然后全局异常处理
相关文章
- java数组乱码_java输入数组结果出现乱码怎么处理[通俗易懂]
- 后端思维篇:统一参数校验、异常处理、结果返回
- ORA-02167: LOGFILE clause specified more than once ORACLE 报错 故障修复 远程处理
- ORA-31446: this session does not own the lock handle for string ORACLE 报错 故障修复 远程处理
- ORA-42030: cannot online redefine table “string”.”string” with commit SCN-based MV log ORACLE 报错 故障修复 远程处理
- ORA-48102: encountered the end-of-file when reading the file ORACLE 报错 故障修复 远程处理
- ORA-48415: Syntax error found in string [string] at column [string] ORACLE 报错 故障修复 远程处理
- Oracle 如何处理Index索引无效的与解决方法及问题原因
- MySQL Error number: 4151; Symbol: ER_BULK_PARSER_INCOMPLETE_ESCAPE_SEQUENCE; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-02751: osnfsmmap: cannot map shared memory file ORACLE 报错 故障修复 远程处理
- ORA-06302: IPA: Cannot connect to remote host ORACLE 报错 故障修复 远程处理
- ORA-09708: soacon: failed to bind socket to port. ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-013007; Symbol: ER_IB_MSG_1182; SQLSTATE: HY000 报错 故障修复 远程处理
- 的处理linux下处理.a文件的方法(linux下.a文件)
- MySQL大数据处理技术研究(mysql处理大数据)
- Oracle数据库查询结果为空,该如何处理?(oracle查询数据为空)
- 结果集处理Oracle数据库中Fetch结果集的技巧(oracle中fetch)
- Oracle中多个结果集合的处理技巧(oracle 多个结果集)
- 接MSSQL查询结果的拼接处理技巧(mssql 查询结果拼)
- SQL Server查询结果的智能处理(sqlserver结果集)
- Oracle中遇到结果为0的处理方法(oracle中结果为0时)