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

View File

@ -83,7 +83,7 @@ public class DecryptingOncePerRequestFilter extends OncePerRequestFilter {
@Override
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() {
@Override
public boolean isFinished() {

View File

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

View File

@ -57,7 +57,7 @@ public class R<T> implements Serializable {
}
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;
}).orElse(Boolean.FALSE);
}
@ -103,7 +103,7 @@ public class R<T> implements Serializable {
}
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) {

View File

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

View File

@ -93,7 +93,7 @@ public class AdminRedisTemplate {
* @return
*/
public String randomKey() {
return (String) redisTemplate.randomKey();
return redisTemplate.randomKey();
}
/**
@ -725,7 +725,7 @@ public class AdminRedisTemplate {
* @return 返回集合中多个随机数
*/
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() {
return zSetOps;
}
}
}

View File

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

View File

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