添加Package
在sikiro-system-api项目添加新package-com.sikiro.system.api,同时添加SikiroSystemApiApplication
@SpringBootApplication() @ComponentScan(basePackages = {"com.sikiro.system"}) public class SikiroSystemApiApplication { public static void main(String[] args) { SpringApplication.run(SikiroSystemApiApplication.class, args); } }
然后继续添加HelloController到新的package-com.sikiro.system.api.controller
@RestController
public class HelloController { @GetMapping("/api/hi") public ApiResult Hi(){ return ApiResult.Success(); } } 在sikiro-system-infrastructure添加下面两个类 package com.sikiro.system.infrastructure.base; import lombok.AccessLevel; import lombok.Setter; /** * API统一响应实体 * @param <T> 业务实体 */ public class ApiResult<T> { @Setter(AccessLevel.PRIVATE) public int resultCode; @Setter(AccessLevel.PRIVATE) public T data; @Setter(AccessLevel.PRIVATE) public String message; @Setter(AccessLevel.PRIVATE) public boolean success; @Setter(AccessLevel.PRIVATE) public boolean failed; @Setter(AccessLevel.PRIVATE) public boolean error; public static <T> ApiResult<T> Success() { return Success(ApiResultCode.Succeed.Message(), null, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Success(T data) { return Success(ApiResultCode.Succeed.Message(), data, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Success(String msg) { return Success(msg, null); } public static <T> ApiResult<T> Success(String msg, T data, int code) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setData(data); result.setResultCode(code); result.setSuccess(true); result.setFailed(false); result.setError(false); return result; } public static <T> ApiResult<T> Success(String msg, T data) { return Success(msg, data, ApiResultCode.Succeed.Value()); } public static <T> ApiResult<T> Failed() { return Failed(ApiResultCode.Failed.Message(), null, ApiResultCode.Failed.Value()); } public static <T> ApiResult<T> Failed(String msg) { return Failed(msg, null); } public static <T> ApiResult<T> Failed(String msg, T data) { return Failed(msg, data, ApiResultCode.Failed.Value()); } public static <T> ApiResult<T> Failed(String msg, T data, int code) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setData(data); result.setResultCode(code); result.setSuccess(false); result.setFailed(true); result.setError(false); return result; } public static <T> ApiResult<T> Error(String msg) { ApiResult<T> result = new ApiResult<>(); result.setMessage(msg); result.setResultCode(ApiResultCode.Error.Value()); result.setSuccess(false); result.setFailed(true); result.setError(true); return result; } } package com.sikiro.system.infrastructure.base; public enum ApiResultCode { Succeed(200,"请求成功"), Failed(400,"请求失败"), Error(500,"服务执行异常"); private int _index; private String _msg; ApiResultCode(int index, String msg) { _index = index; _msg = msg; } public int Value() { return _index; } public String Message() { return _msg; } }
最后编辑启动配置