使用 UnifiedResponse 替代 RestfulResult

dev
ZhouXY108 2023-09-09 18:38:09 +08:00
parent 208d7c93c9
commit b25d7589c2
11 changed files with 72 additions and 68 deletions

View File

@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
*
@ -62,7 +62,7 @@ public class DefaultExceptionHandler extends BaseExceptionHandler {
DataAccessException.class,
MethodArgumentNotValidException.class
})
public ResponseEntity<RestfulResult> handleException(Exception e) {
public ResponseEntity<UnifiedResponse> handleException(Exception e) {
log.error(e.getMessage(), e);
return buildExceptionResponse(e);
}

View File

@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
import xyz.zhouxy.plusone.exception.SysException;
@RestControllerAdvice
@ -21,9 +21,9 @@ public class SysExceptionHandler extends BaseExceptionHandler {
}
@ExceptionHandler({ SysException.class })
public ResponseEntity<RestfulResult> handleException(@Nonnull Exception e) {
public ResponseEntity<UnifiedResponse> handleException(@Nonnull Exception e) {
log.error(e.getMessage(), e);
HttpStatus httpStatus = getHttpStatus(e);
return new ResponseEntity<>(RestfulResult.error(getErrorCode(e), "系统错误"), httpStatus);
return new ResponseEntity<>(UnifiedResponse.error(getErrorCode(e), "系统错误"), httpStatus);
}
}

View File

@ -1,22 +1,26 @@
package xyz.zhouxy.plusone.system.application.common.exception.handler;
import java.util.Objects;
import javax.annotation.Nonnull;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
import xyz.zhouxy.plusone.system.application.common.exception.AccountLoginException;
@RestControllerAdvice
public class AccountLoginExceptionHandler extends BaseExceptionHandler {
public AccountLoginExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public AccountLoginExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
}
@ExceptionHandler({ AccountLoginException.class })
public ResponseEntity<RestfulResult> handleException(Exception e) {
return buildExceptionResponse(e);
public ResponseEntity<UnifiedResponse> handleException(Exception e) {
return buildExceptionResponse(Objects.requireNonNull(e));
}
}

View File

@ -17,7 +17,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.SameTokenInvalidException;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
* Sa-Token
@ -52,7 +52,7 @@ public class SaTokenExceptionHandler extends BaseExceptionHandler {
}
@ExceptionHandler(SaTokenException.class)
public ResponseEntity<RestfulResult> handleSaTokenException(SaTokenException e) {
public ResponseEntity<UnifiedResponse> handleSaTokenException(SaTokenException e) {
log.error(e.getMessage(), e);
return buildExceptionResponse(e);
}

View File

@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
import xyz.zhouxy.plusone.system.application.service.AccountContextService;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
@ -28,34 +28,34 @@ public class AccountContextController {
}
@GetMapping("info")
public RestfulResult getAccountInfo() {
public UnifiedResponse getAccountInfo() {
adminAuthLogic.checkLogin();
var result = service.getAccountInfo();
return RestfulResult.success("查询成功", result);
return UnifiedResponse.success("查询成功", result);
}
@GetMapping("logout")
public RestfulResult logout() {
public UnifiedResponse logout() {
service.logout();
return RestfulResult.success("注销成功");
return UnifiedResponse.success("注销成功");
}
@GetMapping("menus")
public RestfulResult getMenuTree() {
public UnifiedResponse getMenuTree() {
adminAuthLogic.checkLogin();
var result = service.getMenuTree();
return RestfulResult.success("查询成功", result);
return UnifiedResponse.success("查询成功", result);
}
@PostMapping("changePassword")
public RestfulResult changePassword(ChangePasswordCommand command) {
public UnifiedResponse changePassword(ChangePasswordCommand command) {
service.changePassword(command);
return RestfulResult.success("修改成功,请重新登录。");
return UnifiedResponse.success("修改成功,请重新登录。");
}
@PostMapping("changePasswordWithoutLogin")
public RestfulResult changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) {
public UnifiedResponse changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) {
service.changePasswordWithoutLogin(command);
return RestfulResult.success("修改成功,请重新登录。");
return UnifiedResponse.success("修改成功,请重新登录。");
}
}

View File

@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.UnifiedResponse.success;
import java.util.List;
@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.service.AccountManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateAccountCommand;
@ -38,7 +38,7 @@ public class AccountManagementController {
}
@PostMapping
public RestfulResult createAccount(@RequestBody @Valid CreateAccountCommand command) {
public UnifiedResponse createAccount(@RequestBody @Valid CreateAccountCommand command) {
adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-create");
service.createAccount(command);
@ -46,7 +46,7 @@ public class AccountManagementController {
}
@DeleteMapping
public RestfulResult deleteAccounts(@RequestBody List<Long> ids) {
public UnifiedResponse deleteAccounts(@RequestBody List<Long> ids) {
adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-delete");
service.deleteAccounts(ids);
@ -54,7 +54,7 @@ public class AccountManagementController {
}
@PatchMapping("{id}")
public RestfulResult updateAccountInfo(
public UnifiedResponse updateAccountInfo(
@PathVariable("id") Long id,
@RequestBody @Valid UpdateAccountCommand command) {
adminAuthLogic.checkLogin();
@ -64,7 +64,7 @@ public class AccountManagementController {
}
@GetMapping("query")
public RestfulResult queryAccountOverviewList(AccountQueryParams queryParams) {
public UnifiedResponse queryAccountOverviewList(AccountQueryParams queryParams) {
adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-list");
var accountOverviewList = service.queryAccountOverviewList(queryParams);
@ -72,7 +72,7 @@ public class AccountManagementController {
}
@GetMapping("{accountId}")
public RestfulResult queryAccountDetails(@PathVariable("accountId") Long accountId) {
public UnifiedResponse queryAccountDetails(@PathVariable("accountId") Long accountId) {
adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-details");
var accountDetails = service.queryAccountDetails(accountId);

View File

@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.UnifiedResponse.success;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.AdminLoginService;
import xyz.zhouxy.plusone.system.application.service.command.LoginByOtpCommand;
import xyz.zhouxy.plusone.system.application.service.command.LoginByPasswordCommand;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
* Admin
@ -30,19 +30,19 @@ public class AdminLoginController {
}
@PostMapping("byPassword")
public RestfulResult loginByPassword(@RequestBody LoginByPasswordCommand command) {
public UnifiedResponse loginByPassword(@RequestBody LoginByPasswordCommand command) {
var loginInfo = service.loginByPassword(command);
return success("登录成功", loginInfo);
}
@PostMapping("byOtp")
public RestfulResult loginByOtp(@RequestBody LoginByOtpCommand command) {
public UnifiedResponse loginByOtp(@RequestBody LoginByOtpCommand command) {
var loginInfo = service.loginByOtp(command);
return success("登录成功", loginInfo);
}
@GetMapping("sendOtp")
public RestfulResult sendOtp(@RequestParam String principal) {
public UnifiedResponse sendOtp(@RequestParam String principal) {
service.sendOtp(principal);
return success("发送成功");
}

View File

@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.UnifiedResponse.success;
import java.util.List;
@ -20,7 +20,7 @@ import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams;
import xyz.zhouxy.plusone.system.application.service.DictManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateDictCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateDictCommand;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
*
@ -38,21 +38,21 @@ public class DictManagementController {
}
@PostMapping
public RestfulResult createDict(@RequestBody @Valid CreateDictCommand command) {
public UnifiedResponse createDict(@RequestBody @Valid CreateDictCommand command) {
adminAuthLogic.checkPermission("sys-dict-create");
service.createDict(command);
return success();
}
@DeleteMapping
public RestfulResult deleteDicts(@RequestBody List<Long> ids) {
public UnifiedResponse deleteDicts(@RequestBody List<Long> ids) {
adminAuthLogic.checkPermission("sys-dict-delete");
service.deleteDicts(ids);
return success();
}
@PatchMapping("{id}")
public RestfulResult updateDict(
public UnifiedResponse updateDict(
@PathVariable("id") Long id,
@RequestBody @Valid UpdateDictCommand command) {
adminAuthLogic.checkPermission("sys-dict-update");
@ -61,21 +61,21 @@ public class DictManagementController {
}
@GetMapping("{dictId}")
public RestfulResult findDictDetails(@PathVariable("dictId") Long dictId) {
public UnifiedResponse findDictDetails(@PathVariable("dictId") Long dictId) {
adminAuthLogic.checkPermission("sys-dict-details");
var dictDetails = service.findDictDetails(dictId);
return success("查询成功", dictDetails);
}
@GetMapping("all")
public RestfulResult loadAllDicts() {
public UnifiedResponse loadAllDicts() {
adminAuthLogic.checkPermissionAnd("sys-dict-list", "sys-dict-details");
var dicts = service.loadAllDicts();
return success("查询成功", dicts);
}
@GetMapping("query")
public RestfulResult queryDictOverviewList(@Valid DictQueryParams queryParams) {
public UnifiedResponse queryDictOverviewList(@Valid DictQueryParams queryParams) {
adminAuthLogic.checkPermission("sys-dict-list");
var dicts = service.queryDictOverviewList(queryParams);
return success("查询成功", dicts);

View File

@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.UnifiedResponse.success;
import javax.validation.Valid;
@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.MenuManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateMenuCommand;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
*
@ -37,7 +37,7 @@ public class MenuManagementController {
// ==================== create ====================
@PostMapping
public RestfulResult createMenu(@RequestBody @Valid CreateMenuCommand command) {
public UnifiedResponse createMenu(@RequestBody @Valid CreateMenuCommand command) {
adminAuthLogic.checkPermission("sys-menu-create");
service.createMenu(command);
return success();
@ -45,7 +45,7 @@ public class MenuManagementController {
// ==================== delete ====================
@DeleteMapping("{id}")
public RestfulResult deleteMenu(@PathVariable("id") Long id) {
public UnifiedResponse deleteMenu(@PathVariable("id") Long id) {
adminAuthLogic.checkPermission("sys-menu-delete");
service.deleteMenu(id);
return success();
@ -53,7 +53,7 @@ public class MenuManagementController {
// ==================== update ====================
@PatchMapping("{id}")
public RestfulResult updateMenu(
public UnifiedResponse updateMenu(
@PathVariable("id") Long id,
@RequestBody @Valid UpdateMenuCommand command) {
adminAuthLogic.checkPermission("sys-menu-update");
@ -63,21 +63,21 @@ public class MenuManagementController {
// ==================== query ====================
@GetMapping("{id}")
public RestfulResult findById(@PathVariable("id") Long id) {
public UnifiedResponse findById(@PathVariable("id") Long id) {
adminAuthLogic.checkPermission("sys-menu-details");
var result = service.findById(id);
return RestfulResult.success("查询成功", result);
return UnifiedResponse.success("查询成功", result);
}
@GetMapping("queryByAccountId")
public RestfulResult queryByAccountId(@RequestParam Long accountId) {
public UnifiedResponse queryByAccountId(@RequestParam Long accountId) {
adminAuthLogic.checkPermission("sys-menu-details");
var result = service.queryByAccountId(accountId);
return success("查询成功", result);
}
@GetMapping("queryByRoleId")
public RestfulResult queryByRoleId(@RequestParam Long roleId) {
public UnifiedResponse queryByRoleId(@RequestParam Long roleId) {
adminAuthLogic.checkPermission("sys-menu-details");
var result = service.queryByRoleId(roleId);
return success("查询成功", result);

View File

@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.UnifiedResponse.success;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.RegisterAccountService;
import xyz.zhouxy.plusone.system.application.service.command.RegisterAccountCommand;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
*
@ -29,13 +29,13 @@ public class RegisterAccountController {
}
@PostMapping
public RestfulResult registerAccount(@RequestBody RegisterAccountCommand command) {
public UnifiedResponse registerAccount(@RequestBody RegisterAccountCommand command) {
service.registerAccount(command);
return success("注册成功");
}
@GetMapping("sendCode")
public RestfulResult sendCode(@RequestParam String principal) {
public UnifiedResponse sendCode(@RequestParam String principal) {
service.sendCode(principal);
return success("发送成功");
}

View File

@ -17,7 +17,7 @@ import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
import xyz.zhouxy.plusone.system.application.service.RoleManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateRoleCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateRoleCommand;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.UnifiedResponse;
/**
*
@ -35,44 +35,44 @@ public class RoleManagementController {
}
@PostMapping
public RestfulResult createRole(@RequestBody @Valid CreateRoleCommand command) {
public UnifiedResponse createRole(@RequestBody @Valid CreateRoleCommand command) {
adminAuthLogic.checkPermission("sys-role-create");
service.createRole(command);
return RestfulResult.success();
return UnifiedResponse.success();
}
@PatchMapping
public RestfulResult updateRole(@RequestBody @Valid UpdateRoleCommand command) {
public UnifiedResponse updateRole(@RequestBody @Valid UpdateRoleCommand command) {
adminAuthLogic.checkPermission("sys-role-update");
service.updateRole(command);
return RestfulResult.success();
return UnifiedResponse.success();
}
@DeleteMapping("{id}")
public RestfulResult delete(@PathVariable("id") Long id) {
public UnifiedResponse delete(@PathVariable("id") Long id) {
adminAuthLogic.checkPermission("sys-role-delete");
service.delete(id);
return RestfulResult.success();
return UnifiedResponse.success();
}
@GetMapping("exists")
public RestfulResult exists(@RequestParam("id") Long id) {
public UnifiedResponse exists(@RequestParam("id") Long id) {
adminAuthLogic.checkPermissionOr("sys-role-list", "sys-role-details");
var isExists = service.exists(id);
return RestfulResult.success(isExists ? "存在" : "不存在", isExists);
return UnifiedResponse.success(isExists ? "存在" : "不存在", isExists);
}
@GetMapping("{id}")
public RestfulResult findById(@PathVariable("id") Long id) {
public UnifiedResponse findById(@PathVariable("id") Long id) {
adminAuthLogic.checkPermission("sys-role-details");
var result = service.findById(id);
return RestfulResult.success("查询成功", result);
return UnifiedResponse.success("查询成功", result);
}
@GetMapping("query")
public RestfulResult query(RoleQueryParams params) {
public UnifiedResponse query(RoleQueryParams params) {
adminAuthLogic.checkPermission("sys-role-list");
var result = service.query(params);
return RestfulResult.success("查询成功", result);
return UnifiedResponse.success("查询成功", result);
}
}