Commit 0b164889 by kenzo

update apiResult

parent 517de7c9
...@@ -30,6 +30,11 @@ ...@@ -30,6 +30,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
......
package com.cnooc.expert; package com.cnooc.expert;
import com.cnooc.expert.common.response.ApiResult; import com.cnooc.expert.common.response.ApiResult;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@AllArgsConstructor
@RestController @RestController
public class Ping { public class Ping {
private final StringRedisTemplate redisTemplate;
@GetMapping("/ping") @GetMapping("/ping")
public ApiResult<String> ping() { public ApiResult<String> ping() {
return ApiResult.successWithData("Pong"); redisTemplate.opsForValue().set("ping", "Pong");
return ApiResult.successWithResult("Pong");
} }
} }
...@@ -8,14 +8,14 @@ public class ErrorCode { ...@@ -8,14 +8,14 @@ public class ErrorCode {
/** /**
* 错误码 * 错误码
*/ */
private final String code; private final Integer code;
/** /**
* 错误提示 * 错误提示
*/ */
private final String msg; private final String msg;
public ErrorCode(String code, String msg) { public ErrorCode(int code, String msg) {
this.code = code; this.code = code;
this.msg = msg; this.msg = msg;
} }
......
...@@ -2,45 +2,45 @@ package com.cnooc.expert.common.exception; ...@@ -2,45 +2,45 @@ package com.cnooc.expert.common.exception;
public interface GlobalErrorCodeConstants { public interface GlobalErrorCodeConstants {
ErrorCode SUCCESS = new ErrorCode("200", "成功"); ErrorCode SUCCESS = new ErrorCode(100200, "操作成功");
// ========== 系统级别错误 (1000-1999) ========== // ========== 系统级别错误 (1000-1999) ==========
ErrorCode BAD_REQUEST = new ErrorCode("1001", "请求参数不正确"); ErrorCode BAD_REQUEST = new ErrorCode(1001, "请求参数不正确");
ErrorCode UNAUTHORIZED = new ErrorCode("1002", "账号未登录"); ErrorCode UNAUTHORIZED = new ErrorCode(1002, "账号未登录");
ErrorCode FORBIDDEN = new ErrorCode("1003", "没有该操作权限"); ErrorCode FORBIDDEN = new ErrorCode(1003, "没有该操作权限");
ErrorCode NOT_FOUND = new ErrorCode("1004", "请求未找到"); ErrorCode NOT_FOUND = new ErrorCode(1004, "请求未找到");
ErrorCode METHOD_NOT_ALLOWED = new ErrorCode("1005", "请求方法不正确"); ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(1005, "请求方法不正确");
ErrorCode LOCKED = new ErrorCode("1006", "请求失败,请稍后重试"); ErrorCode LOCKED = new ErrorCode(1006, "请求失败,请稍后重试");
ErrorCode TOO_MANY_REQUESTS = new ErrorCode("1007", "请求过于频繁,请稍后重试"); ErrorCode TOO_MANY_REQUESTS = new ErrorCode(1007, "请求过于频繁,请稍后重试");
ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode("1008", "系统异常"); ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(1008, "系统异常");
ErrorCode NOT_IMPLEMENTED = new ErrorCode("1009", "功能未实现/未开启"); ErrorCode NOT_IMPLEMENTED = new ErrorCode(1009, "功能未实现/未开启");
ErrorCode ERROR_CONFIGURATION = new ErrorCode("1010", "错误的配置项"); ErrorCode ERROR_CONFIGURATION = new ErrorCode(1010, "错误的配置项");
ErrorCode CAPTCHA_EXPIRED = new ErrorCode("1011", "验证码错误"); ErrorCode CAPTCHA_EXPIRED = new ErrorCode(1011, "验证码错误");
ErrorCode CODE_REDIS_KEY = new ErrorCode("1011", "验证码已过期"); ErrorCode CODE_REDIS_KEY = new ErrorCode(1011, "验证码已过期");
ErrorCode SYSTEM_ERROR = new ErrorCode("1012", "系统内部异常"); ErrorCode SYSTEM_ERROR = new ErrorCode(1012, "系统内部异常");
// ========== 用户认证相关错误 (2000-2999) ========== // ========== 用户认证相关错误 (2000-2999) ==========
ErrorCode LOGIN_EXPIRED = new ErrorCode("2001", "请先登录"); ErrorCode LOGIN_EXPIRED = new ErrorCode(2001, "请先登录");
ErrorCode USER_NOT_EXISTS = new ErrorCode("2002", "用户不存在"); ErrorCode USER_NOT_EXISTS = new ErrorCode(2002, "用户不存在");
ErrorCode PASSWORD_ERROR = new ErrorCode("2003", "密码错误"); ErrorCode PASSWORD_ERROR = new ErrorCode(2003, "密码错误");
ErrorCode USER_DISABLED = new ErrorCode("2004", "用户已被禁用"); ErrorCode USER_DISABLED = new ErrorCode(2004, "用户已被禁用");
// ========== 参数校验错误 (3000-3999) ========== // ========== 参数校验错误 (3000-3999) ==========
ErrorCode PARAM_REQUIRED = new ErrorCode("3001", "必填字段不能为空"); ErrorCode PARAM_REQUIRED = new ErrorCode(3001, "必填字段不能为空");
ErrorCode PARAM_FORMAT_ERROR = new ErrorCode("3002", "参数格式不正确"); ErrorCode PARAM_FORMAT_ERROR = new ErrorCode(3002, "参数格式不正确");
ErrorCode PARAM_RANGE_ERROR = new ErrorCode("3003", "参数超出范围"); ErrorCode PARAM_RANGE_ERROR = new ErrorCode(3003, "参数超出范围");
// ========== 业务逻辑错误 (4000-4999) ========== // ========== 业务逻辑错误 (4000-4999) ==========
ErrorCode OPERATION_TOO_FREQUENT = new ErrorCode("4001", "操作太频繁,请稍后再试"); ErrorCode OPERATION_TOO_FREQUENT = new ErrorCode(4001, "操作太频繁,请稍后再试");
ErrorCode DATA_ALREADY_EXISTS = new ErrorCode("4002", "数据已存在"); ErrorCode DATA_ALREADY_EXISTS = new ErrorCode(4002, "数据已存在");
ErrorCode DATA_NOT_EXISTS = new ErrorCode("4003", "数据不存在"); ErrorCode DATA_NOT_EXISTS = new ErrorCode(4003, "数据不存在");
ErrorCode BUSINESS_FAILED = new ErrorCode("4004", "业务处理失败"); ErrorCode BUSINESS_FAILED = new ErrorCode(4004, "业务处理失败");
// ========== 数据访问错误 (5000-5999) ========== // ========== 数据访问错误 (5000-5999) ==========
ErrorCode DATABASE_CONNECTION_ERROR = new ErrorCode("5001", "数据库连接失败"); ErrorCode DATABASE_CONNECTION_ERROR = new ErrorCode(5001, "数据库连接失败");
ErrorCode QUERY_DATA_ERROR = new ErrorCode("5002", "查询数据出错"); ErrorCode QUERY_DATA_ERROR = new ErrorCode(5002, "查询数据出错");
ErrorCode INSERT_DATA_ERROR = new ErrorCode("5003", "插入数据出错"); ErrorCode INSERT_DATA_ERROR = new ErrorCode(5003, "插入数据出错");
ErrorCode UPDATE_DATA_ERROR = new ErrorCode("5004", "更新数据出错"); ErrorCode UPDATE_DATA_ERROR = new ErrorCode(5004, "更新数据出错");
ErrorCode DELETE_DATA_ERROR = new ErrorCode("5005", "删除数据出错"); ErrorCode DELETE_DATA_ERROR = new ErrorCode(5005, "删除数据出错");
} }
...@@ -48,18 +48,18 @@ public class GlobalExceptionHandler { ...@@ -48,18 +48,18 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ApiResult<String> handleException(Exception e, HttpServletRequest request) { public ApiResult<String> handleException(Exception e, HttpServletRequest request) {
log.error("系统异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e); log.error("系统异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e);
return ApiResult.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), GlobalErrorCodeConstants.SYSTEM_ERROR.getMsg()); return ApiResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), GlobalErrorCodeConstants.SYSTEM_ERROR.getMsg());
} }
@ExceptionHandler(BusinessException.class) @ExceptionHandler(BusinessException.class)
public ApiResult<String> handleBusinessException(BusinessException e, HttpServletRequest request) { public ApiResult<String> handleBusinessException(BusinessException e, HttpServletRequest request) {
log.error("业务异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e); log.error("业务异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e);
return ApiResult.error(e.getMessage(), e.getMessage()); return ApiResult.error(e.getErrorCode().getCode(), e.getMessage());
} }
@ExceptionHandler(NullPointerException.class) @ExceptionHandler(NullPointerException.class)
public ApiResult<String> handleNullPointerException(NullPointerException e, HttpServletRequest request) { public ApiResult<String> handleNullPointerException(NullPointerException e, HttpServletRequest request) {
log.error("空指针异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e); log.error("空指针异常: {},请求URL: {}", e.getMessage(), request.getRequestURI(), e);
return ApiResult.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), GlobalErrorCodeConstants.SYSTEM_ERROR.getMsg()); return ApiResult.error(GlobalErrorCodeConstants.SYSTEM_ERROR.getCode(), GlobalErrorCodeConstants.SYSTEM_ERROR.getMsg());
} }
} }
package com.cnooc.expert.common.response; package com.cnooc.expert.common.response;
import com.cnooc.expert.common.exception.ErrorCode;
import com.cnooc.expert.common.exception.GlobalErrorCodeConstants;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -14,7 +16,7 @@ public class ApiResult<T> implements Serializable { ...@@ -14,7 +16,7 @@ public class ApiResult<T> implements Serializable {
/** /**
* 响应码 * 响应码
*/ */
private String code; private Integer httpCode;
/** /**
* 响应消息 * 响应消息
...@@ -24,45 +26,45 @@ public class ApiResult<T> implements Serializable { ...@@ -24,45 +26,45 @@ public class ApiResult<T> implements Serializable {
/** /**
* 响应数据 * 响应数据
*/ */
private T data; private T result;
public ApiResult(String code, String message, T data) { public ApiResult(int httpCode, String message, T result) {
this.code = code; this.httpCode = httpCode;
this.message = message; this.message = message;
this.data = data; this.result = result;
} }
/** /**
* 成功返回结果 * 成功返回结果
*/ */
public static <T> ApiResult<T> successWithData(T data) { public static <T> ApiResult<T> successWithResult(T result) {
return new ApiResult<>("200", "操作成功", data); return new ApiResult<>(GlobalErrorCodeConstants.SUCCESS.getCode(), GlobalErrorCodeConstants.SUCCESS.getMsg(), result);
} }
/** /**
* 成功返回结果 * 成功返回结果
*/ */
public static <T> ApiResult<T> success() { public static <T> ApiResult<T> success() {
return new ApiResult<>("200", "操作成功", null); return new ApiResult<>(GlobalErrorCodeConstants.SUCCESS.getCode(), GlobalErrorCodeConstants.SUCCESS.getMsg(), null);
} }
public static <T> ApiResult<T> success(String message) { public static <T> ApiResult<T> success(String message) {
return new ApiResult<>("200", message, null); return new ApiResult<>(GlobalErrorCodeConstants.SUCCESS.getCode(), message, null);
} }
/** /**
* 失败返回结果 * 失败返回结果
*/ */
public static <T> ApiResult<T> error(String message) { public static <T> ApiResult<T> error(String message) {
return new ApiResult<>("500", message, null); return new ApiResult<>(GlobalErrorCodeConstants.SYSTEM_ERROR.getCode(), message, null);
} }
/** /**
* 失败返回结果 * 失败返回结果
*/ */
public static <T> ApiResult<T> error(String code, String message) { public static <T> ApiResult<T> error(int httpCode, String message) {
return new ApiResult<>(code, message, null); return new ApiResult<>(httpCode, message, null);
} }
// Getters and Setters // Getters and Setters
......
package com.cnooc.expert.controller.auth;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
@AllArgsConstructor
@RestController
public class AuthController {
}
package com.cnooc.expert.controller.auth;
public class LoginController {
}
package com.cnooc.expert.controller.person; package com.cnooc.expert.controller.person;
import com.cnooc.expert.common.response.ApiResult;
import com.cnooc.expert.controller.person.model.request.PersonGetReq;
import com.cnooc.expert.service.PersonService; import com.cnooc.expert.service.PersonService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -18,9 +15,8 @@ public class PersonController { ...@@ -18,9 +15,8 @@ public class PersonController {
private final PersonService personService; private final PersonService personService;
@GetMapping("/get") @GetMapping("/get")
// public ApiResult<String> personGet(@RequestBody @Validated PersonGetReq req) { public String personGet() throws JsonProcessingException {
public ApiResult<String> personGet() { return personService.getExpertDetail(1);
return ApiResult.successWithData(personService.getExpertDetail(1));
} }
} }
...@@ -12,7 +12,6 @@ import java.util.Map; ...@@ -12,7 +12,6 @@ import java.util.Map;
public interface ExpertServiceApi { public interface ExpertServiceApi {
// GET请求
@GET("/api") @GET("/api")
Call<ExpertInfoGetResp> expertDetailGet(@HeaderMap Map<String, Object> headers); Call<ExpertInfoGetResp> expertDetailGet(@HeaderMap Map<String, Object> headers);
......
package com.cnooc.expert.service; package com.cnooc.expert.service;
import com.cnooc.expert.controller.person.model.request.PersonGetReq;
import com.cnooc.expert.external.expert.model.response.ExpertInfoGetResp;
import com.cnooc.expert.external.expert.service.ExpertService; import com.cnooc.expert.external.expert.service.ExpertService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@AllArgsConstructor @AllArgsConstructor
@Service @Service
public class PersonService { public class PersonService {
private ObjectMapper objectMapper;
private final ExpertService expertService; private final ExpertService expertService;
public String getExpertDetail(int id) { public String getExpertDetail(int id) throws JsonProcessingException {
ExpertInfoGetResp expertInfo = expertService.getExpertInfo(id); return objectMapper.writeValueAsString(expertService.getExpertInfo(id));
return StringUtils.defaultIfBlank(expertInfo.getInfo().getSeed(), "") ;
} }
} }
spring:
redis:
host: 39.105.210.213
port: 6379
database: 6
password: 'WSzs*aA^U2nH$u!K'
lettuce:
cluster:
refresh:
adaptive: true
period: 30000
pool:
max-wait: 10000
max-idle: 10
min-idle: 10
max-active: 20
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
indent-output: true
deserialization:
fail-on-unknown-properties: false
default-property-inclusion: non_null
server: server:
port: 9090 port: 9090
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment