其他
通用返回结果类
```java package person.rootwhois.blog.common.result;
/**
* @Author: 陈广生
* @Date: 2022/01/06/10:25 AM
* @Description:
*/
public enum ApiErrorCode {
/**
* 失败
*/
FAILED(1, "数据获取失败"),
/**
* 服务异常
*/
SERVICE(2, "服务器异常"),
/**
* 成功
*/
SUCCESS(0, "数据获取成功");
private final long code;
private final String msg;
ApiErrorCode(final long code, final String msg) {
this.code = code;
this.msg = msg;
}
public static ApiErrorCode fromCode(long code) {
ApiErrorCode[] ecs = ApiErrorCode.values();
for (ApiErrorCode ec : ecs) {
if (ec.getCode() == code) {
return ec;
}
}
return SUCCESS;
}
public long getCode() {
return code;
}
public String getMsg() {
return msg;
}
@Override
public String toString() {
return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
}
}
```java
package person.rootwhois.blog.common.result;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
/**
* @Author: 陈广生
* @Date: 2022/01/06/10:35 AM
* @Description:
*/
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
/**
* 业务错误码
*/
private long code;
/**
* 描述
*/
private String msg;
/**
* 结果集
*/
private T data;
/**
* 响应时间
*/
private String responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
public Result() {
}
public Result(ApiErrorCode errorCode) {
errorCode = Optional.ofNullable(errorCode).orElse(ApiErrorCode.FAILED);
this.code = errorCode.getCode();
this.msg = errorCode.getMsg();
}
public static <T> Result<T> ok(T data) {
ApiErrorCode aec = ApiErrorCode.SUCCESS;
if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
aec = ApiErrorCode.FAILED;
}
return restResult(data, aec);
}
public static <T> Result<T> failed(String msg) {
return restResult(null, ApiErrorCode.FAILED.getCode(), msg);
}
public static <T> Result<T> failed(ApiErrorCode errorCode) {
return restResult(null, errorCode);
}
public static <T> Result<T> restResult(T data, ApiErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMsg());
}
private static <T> Result<T> restResult(T data, long code, String msg) {
Result<T> apiResult = new Result<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public boolean ok() {
return ApiErrorCode.SUCCESS.getCode() == code;
}
/**
* 服务间调用非业务正常,异常直接释放
*/
public T serviceData() throws Exception {
if (!ok()) {
throw new Exception(this.msg);
}
return data;
}
}
单元测试
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc class AdminControllerTest { @Autowired private MockMvc mockMvc; private static final String ORIGIN = "*"; private static final String ACCESS_CONTROL_REQUEST_HEADERS = "*"; private static final String TOKEN = ""; private static final String URL = "/api"; private static final Integer CODE = 1; @Test void test() throws Exception { String url = "/test"; String json = "{\"id\":1}"; doResult(sendPost(url, json), true); } /** * 上传文件请求 * @param url url * @param filePath filePath * @throws Exception e */ private ResultActions sendPostFile(String url, String fileName, String fileType, String filePath) throws Exception { String originalFileName = filePath.substring(filePath.lastIndexOf("\\") + 1); return mockMvc.perform(MockMvcRequestBuilders.fileUpload(URL + url) .file(new MockMultipartFile(fileName, originalFileName, fileType, new FileInputStream(filePath))) .header("Origin", ORIGIN) .header("Access-Control-Request-Headers", ACCESS_CONTROL_REQUEST_HEADERS) .header("Authorization", TOKEN)); } /** * post请求 * * @param url url * @throws Exception e */ private ResultActions sendPost(String url) throws Exception { return mockMvc.perform(MockMvcRequestBuilders.post(URL + url) .header("Origin", ORIGIN) .header("Access-Control-Request-Headers", ACCESS_CONTROL_REQUEST_HEADERS) .header("Authorization", TOKEN) .contentType("application/json;charset=UTF-8")); } /** * 带参post请求 * * @param url url * @param json json字串 * @throws Exception e */ private ResultActions sendPost(String url, String json) throws Exception { return mockMvc.perform(MockMvcRequestBuilders.post(URL + url) .header("Origin", ORIGIN) .header("Access-Control-Request-Headers", ACCESS_CONTROL_REQUEST_HEADERS) .header("Authorization", TOKEN) .contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE) .contentType("application/json;charset=UTF-8") .content(json)); } /** * 处理结果 * * @param resultActions resultActions * @param needVerifyCode 是否需要校验code * @throws Exception e */ private void doResult(ResultActions resultActions, boolean needVerifyCode) throws Exception { resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8"); resultActions.andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()); if (needVerifyCode) { resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.code").value(CODE)); } } }
@Scheduled
注解可以控制方法定时执行,其中有三个参数可选择:- fixedDelay控制方法执行的间隔时间,是以上一次方法执行完开始算起,如上一次方法执行阻塞住了,那么直到上一次执行完,并间隔给定的时间后,执行下一次。
- fixedRate是按照一定的速率执行,是从上一次方法执行开始的时间算起,如果上一次方法阻塞住了,下一次也是不会执行,但是在阻塞这段时间内累计应该执行的次数,当不再阻塞时,一下子把这些全部执行掉,而后再按照固定速率继续执行。
- cron表达式可以定制化执行任务,但是执行的方式是与fixedDelay相近的,也是会按照上一次方法结束时间开始算起。
- initialDelay 。如:
@Scheduled(initialDelay = 10000,fixedRate = 15000)
这个定时器就是在上一个的基础上加了一个initialDelay = 10000 意思就是在容器启动后,延迟10秒后再执行一次定时器,以后每15秒再执行一次该定时器。
操作日志相关文章和工具。