项目初期配置
2025/7/10大约 1 分钟
项目初期配置
包装返回结果
/**
* 后端统一返回结果
*
* @param <T>
*/
@Data
public class Result<T> implements Serializable {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
public static <T> Result<T> success() {
Result<T> result = new Result<>();
result.code = 1;
return result;
}
public static <T> Result<T> success(T object) {
Result<T> result = new Result<>();
result.data = object;
result.code = 1;
return result;
}
public static <T> Result<T> error(String msg) {
Result<T> result = new Result<>();
result.msg = msg;
result.code = 0;
return result;
}
}
包装分页请求参数
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageParam {
private Integer pageNo;
private Integer pageSize;
}
包装分页请求结果
@Data
@AllArgsConstructor
public class PageResult<T> {
private List<T> list;
private Long total;
}
配置全局异常处理器
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理异常
*
* @param ex
* @return
*/
@ExceptionHandler(Exception.class)
public Result exceptionHandler(Exception ex) {
log.error("未知异常:{}", ex.getMessage(), ex.getCause());
return Result.error(ex.getMessage());
}
}
跨域请求配置
@Slf4j
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://172.0.0.1/")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600);
log.info("跨域请求服务启用");
}
}
最终项目结构如下 :
│ .gitignore
│ pom.xml
│
├─api-gateway-center
│ │ pom.xml
│ │
│ ├─src
│ │ └─main
│ │ ├─java
│ │ │ └─top
│ │ │ └─codelong
│ │ │ └─apigatewaycenter
│ │ │ │ ApiGatewayCenterApplication.java
│ │ │ │
│ │ │ ├─common
│ │ │ │ ├─page
│ │ │ │ │ PageParam.java
│ │ │ │ │ PageResult.java
│ │ │ │ │
│ │ │ │ └─result
│ │ │ │ Result.java
│ │ │ │
│ │ │ ├─config
│ │ │ │ NginxConfig.java
│ │ │ │ UniqueIdConfig.java
│ │ │ │ WebMvcConfig.java
│ │ │ │
│ │ │ ├─controller
│ │ │ │ GatewayGroupController.java
│ │ │ │ GatewayGroupDetailController.java
│ │ │ │ GatewayInterfaceController.java
│ │ │ │ GatewayMethodController.java
│ │ │ │ GatewayServerController.java
│ │ │ │ GatewayServerDetailController.java
│ │ │ │ GatewayServerGroupRelController.java
│ │ │ │
│ │ │ ├─dao
│ │ │ │ ├─entity
│ │ │ │ │ GatewayGroupDetailDO.java
│ │ │ │ │ GatewayGroupDO.java
│ │ │ │ │ GatewayInterfaceDO.java
│ │ │ │ │ GatewayMethodDO.java
│ │ │ │ │ GatewayServerDetail.java
│ │ │ │ │ GatewayServerDO.java
│ │ │ │ │ GatewayServerGroupRelDO.java
│ │ │ │ │
│ │ │ │ └─mapper
│ │ │ │ GatewayGroupDetailMapper.java
│ │ │ │ GatewayGroupMapper.java
│ │ │ │ GatewayInterfaceMapper.java
│ │ │ │ GatewayMethodMapper.java
│ │ │ │ GatewayServerDetailMapper.java
│ │ │ │ GatewayServerGroupRelMapper.java
│ │ │ │ GatewayServerMapper.java
│ │ │ │
│ │ │ ├─dto
│ │ │ │ ├─domain
│ │ │ │ ├─req
│ │ │ │ └─resp
│ │ │ ├─enums
│ │ │ │ StatusEnum.java
│ │ │ │
│ │ │ ├─handler
│ │ │ │ GlobalExceptionHandler.java
│ │ │ │
│ │ │ ├─scheduled
│ │ │ ├─service
│ │ │ │ └─impl
│ │ │ │ │ GatewayGroupDetailService.java
│ │ │ │ │ GatewayGroupService.java
│ │ │ │ │ GatewayInterfaceService.java
│ │ │ │ │ GatewayMethodService.java
│ │ │ │ │ GatewayServerDetailService.java
│ │ │ │ │ GatewayServerGroupRelService.java
│ │ │ │ │ GatewayServerService.java
│ │ │ │ │
│ │ │ │ └─impl
│ │ │ │ GatewayGroupDetailServiceImpl.java
│ │ │ │ GatewayGroupServiceImpl.java
│ │ │ │ GatewayInterfaceServiceImpl.java
│ │ │ │ GatewayMethodServiceImpl.java
│ │ │ │ GatewayServerDetailServiceImpl.java
│ │ │ │ GatewayServerGroupRelServiceImpl.java
│ │ │ │ GatewayServerServiceImpl.java
│ │ │ │
│ │ │ └─utils
│ │ │ UniqueIdUtil.java
│ │ │
│ │ └─resources
│ │ │ application.yml
│ │ │
│ │ └─mapper
│ │ GatewayGroupDetailMapper.xml
│ │ GatewayGroupMapper.xml
│ │ GatewayInterfaceMapper.xml
│ │ GatewayMethodMapper.xml
│ │ GatewayServerDetailMapper.xml
│ │ GatewayServerGroupRelMapper.xml
│ │ GatewayServerMapper.xml
│ │
├─api-gateway-core
│ │ pom.xml
│ │
│ └─src
│ └─main
│ ├─java
│ │ └─top
│ │ └─codelong
│ │ └─apigatewaycore
│ │ ApiGatewayCoreApplication.java
│ │
│ └─resources
│ │ application.properties
│ │
│ ├─static
│ └─templates
├─api-gateway-framework
│ │ pom.xml
│ │
│ ├─api-gateway-auth
│ │ │ pom.xml
│ │ │
│ │ └─src
│ │ ├─main
│ │ │ ├─java
│ │ │ └─resources
│ │ └─test
│ │ └─java
│ └─api-gateway-heartbeat
│ │ pom.xml
│ │
│ └─src
│ ├─main
│ │ ├─java
│ │ └─resources
│ └─test
│ └─java
├─server-find-sdk
│ │ pom.xml
│ │
│ └─src
│ ├─main
│ │ ├─java
│ │ └─resources
│ └─test
│ └─java
└─server-send-sdk
│ pom.xml
│
└─src
├─main
│ ├─java
│ └─resources
└─test
└─java