code cleanup

This commit is contained in:
houwei 2024-06-24 17:38:30 +08:00
parent b2696d06c2
commit 25bb8a6d21
8 changed files with 19 additions and 23 deletions

View File

@ -101,8 +101,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public R handleValidatedException(Exception exception) { public R handleValidatedException(Exception exception) {
BindingResult bindingResult = null; BindingResult bindingResult = null;
if (exception instanceof MethodArgumentNotValidException){ if (exception instanceof MethodArgumentNotValidException e){
MethodArgumentNotValidException e = (MethodArgumentNotValidException) exception;
bindingResult = e.getBindingResult(); bindingResult = e.getBindingResult();
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
// String collect = bindingResult.getAllErrors().stream() // String collect = bindingResult.getAllErrors().stream()
@ -113,14 +112,12 @@ public class GlobalExceptionHandler {
return R.fail(fieldError.getField()+ "" + fieldError.getDefaultMessage()); return R.fail(fieldError.getField()+ "" + fieldError.getDefaultMessage());
} }
} }
}else if (exception instanceof ConstraintViolationException){ }else if (exception instanceof ConstraintViolationException e){
ConstraintViolationException e = (ConstraintViolationException) exception;
String collect = e.getConstraintViolations().stream() String collect = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage) .map(ConstraintViolation::getMessage)
.collect(Collectors.joining(";")); .collect(Collectors.joining(";"));
return R.fail(collect); return R.fail(collect);
}else if (exception instanceof BindException){ }else if (exception instanceof BindException e){
BindException e = (BindException) exception;
bindingResult = e.getBindingResult(); bindingResult = e.getBindingResult();
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
// String collect = bindingResult.getAllErrors().stream() // String collect = bindingResult.getAllErrors().stream()
@ -150,7 +147,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public R handlerNullPointException(NullPointerException exception) { public R handlerNullPointException(NullPointerException exception) {
String message = exception.getMessage(); String message = exception.getMessage();
log.error("全局捕获null错误信息: {}", exception.toString(), exception); log.error("全局捕获null错误信息: {}", exception, exception);
return R.fail(message); return R.fail(message);
} }
@ -185,7 +182,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R handlerBindException(Exception exception) { public R handlerBindException(Exception exception) {
String message = exception.getMessage(); String message = exception.getMessage();
log.error("全局捕获错误信息: {}", exception.toString(), exception); log.error("全局捕获错误信息: {}", exception, exception);
return R.fail(message); return R.fail(message);
} }
} }

View File

@ -83,7 +83,7 @@ public class DecryptingOncePerRequestFilter extends OncePerRequestFilter {
@Override @Override
public ServletInputStream getInputStream() throws IOException { public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(bodyData.getBytes("UTF-8")); final ByteArrayInputStream bais = new ByteArrayInputStream(bodyData.getBytes(StandardCharsets.UTF_8));
return new ServletInputStream() { return new ServletInputStream() {
@Override @Override
public boolean isFinished() { public boolean isFinished() {

View File

@ -22,7 +22,7 @@ public class RequestIdUtils {
} }
public static UUID getRequestId() { public static UUID getRequestId() {
return (UUID)requestIdHolder.get(); return requestIdHolder.get();
} }
public static void removeRequestId() { public static void removeRequestId() {

View File

@ -57,7 +57,7 @@ public class R<T> implements Serializable {
} }
public static boolean isSuccess(@Nullable R<?> result) { public static boolean isSuccess(@Nullable R<?> result) {
return (Boolean) Optional.ofNullable(result).map((x) -> { return Optional.ofNullable(result).map((x) -> {
return ResultCode.SUCCESS.code == x.code; return ResultCode.SUCCESS.code == x.code;
}).orElse(Boolean.FALSE); }).orElse(Boolean.FALSE);
} }
@ -103,7 +103,7 @@ public class R<T> implements Serializable {
} }
public static <T> R<T> fail(int code, String msg) { public static <T> R<T> fail(int code, String msg) {
return new R(code, (Object)null, msg); return new R(code, null, msg);
} }
public static <T> R<T> fail(IResultCode resultCode) { public static <T> R<T> fail(IResultCode resultCode) {

View File

@ -36,7 +36,7 @@ public enum ResultCode implements IResultCode {
return this.message; return this.message;
} }
private ResultCode(final int code, final String message) { ResultCode(final int code, final String message) {
this.code = code; this.code = code;
this.message = message; this.message = message;
} }

View File

@ -93,7 +93,7 @@ public class AdminRedisTemplate {
* @return * @return
*/ */
public String randomKey() { public String randomKey() {
return (String) redisTemplate.randomKey(); return redisTemplate.randomKey();
} }
/** /**
@ -725,7 +725,7 @@ public class AdminRedisTemplate {
* @return 返回集合中多个随机数 * @return 返回集合中多个随机数
*/ */
public List sRandMember(String key, int count) { public List sRandMember(String key, int count) {
return setOps.randomMembers(key, (long) count); return setOps.randomMembers(key, count);
} }
/** /**
@ -952,4 +952,4 @@ public class AdminRedisTemplate {
public ZSetOperations<String, Object> getZSetOps() { public ZSetOperations<String, Object> getZSetOps() {
return zSetOps; return zSetOps;
} }
} }

View File

@ -60,7 +60,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
public static ListableBeanFactory getBeanFactory() { public static ListableBeanFactory getBeanFactory() {
return null == beanFactory ? applicationContext : beanFactory; return null == beanFactory ? applicationContext : beanFactory;
} }
/** /**
* 通过name获取 Bean * 通过name获取 Bean
@ -180,9 +180,8 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
public static <T> void registerBean(String beanName, T bean) { public static <T> void registerBean(String beanName, T bean) {
if(null != beanFactory){ if(null != beanFactory){
beanFactory.registerSingleton(beanName, bean); beanFactory.registerSingleton(beanName, bean);
} else if(applicationContext instanceof ConfigurableApplicationContext){ } else if(applicationContext instanceof ConfigurableApplicationContext context){
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; context.getBeanFactory().registerSingleton(beanName, bean);
context.getBeanFactory().registerSingleton(beanName, bean);
} }
} }
} }

View File

@ -100,7 +100,7 @@ public class LoginServiceImpl implements LoginService {
return loginInfo; return loginInfo;
} }
StpUtil.login(loginUserDetailsVo.getSysUser().getAccount());// 执行登录这里username为用户唯一标识 StpUtil.login(loginUserDetailsVo.getSysUser().getAccount());// 执行登录这里username为用户唯一标识
String newToken = StpUtil.getTokenValue().replace("-", "");; String newToken = StpUtil.getTokenValue().replace("-", "");
String newRefreshTokenUuid = IdUtil.fastSimpleUUID(); String newRefreshTokenUuid = IdUtil.fastSimpleUUID();
String newRefreshToken = "refresh:" + newRefreshTokenUuid; String newRefreshToken = "refresh:" + newRefreshTokenUuid;
loginInfo.setAccessToken(newToken); loginInfo.setAccessToken(newToken);