Master Spring Boot Project Setup: From IDE to Version Management & Common Tools
This guide walks through initializing a Spring Boot project, handling IDE licensing hurdles, ensuring compatible Spring Cloud, Spring Boot, and Kafka versions, leveraging Maven for dependency management, and integrating essential utilities like global exception handling, logging, CORS, Swagger, and helpful development tools.
Project Initialization
Developers often complain that setting up the development environment is the most painful part of a new project. The article walks through creating a Spring Boot project with an IDE, showing the generated structure and starter class.
This is the freshly created project with a startup class, configuration file, and test class.
Version Management
The article highlights the importance of matching Spring Cloud, Spring Boot, and Kafka versions, providing URLs to the official version tables.
A real‑world issue is described where a production Kafka server (0.11) was incompatible with a client library (3.0.4) despite Spring Boot version compatibility, leading to an UnsupportedVersionException.
?,?:Exception thrown when sending a message with key='null' and payload='byte[205]' to topic notify org.apache.kafka.common.errors.UnsupportedVersionException: Attempting to use idempotence with a broker which does not support the required message format (v2). The broker must be version *0.11* or later.The solution is to rely on Maven for dependency management and to understand Spring Boot’s auto‑configuration, which simplifies but does not eliminate version conflicts.
Common Utilities
Examples of useful components are provided, including a global exception handler, request logging aspect, CORS configuration, Swagger setup, and a generic response wrapper.
@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
public ResponseResult<String> handleValidException(MethodArgumentNotValidException ex, HttpServletResponse httpServletResponse) {
log.error("[GlobalExceptionHandler][handleValidException] 参数校验exception", ex);
return wrapperBindingResult(ex.getBindingResult(), httpServletResponse);
}
private ResponseResult<String> wrapperBindingResult(BindingResult bindingResult, HttpServletResponse httpServletResponse) {
StringBuilder errorMsg = new StringBuilder();
for (ObjectError error : bindingResult.getAllErrors()) {
if (error instanceof FieldError) {
errorMsg.append(((FieldError) error).getField()).append(": ");
}
errorMsg.append(error.getDefaultMessage() == null ? "" : error.getDefaultMessage());
}
httpServletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
return ResponseResult.failed(ResultCode.FAILED.getCode(), null);
}
} @Aspect
@Slf4j
@Component
public class WebLogAspect {
@Pointcut("@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.RestController)")
public void cutController() {}
@Before("cutController()")
public void doBefore(JoinPoint point) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String url = request.getRequestURL().toString();
List<Object> list = Lists.newArrayList();
for (Object object : point.getArgs()) {
if (object instanceof MultipartFile || object instanceof HttpServletRequest || object instanceof HttpServletResponse || object instanceof BindingResult) {
continue;
}
list.add(object);
}
log.info("请求 uri:[{}],params:[{}]", url, StringUtils.join(list, ","));
}
@AfterReturning(returning = "response", pointcut = "cutController()")
public void doAfterReturning(Object response) {
if (response != null) {
log.info("请求返回result:[{}]", JSONUtil.toJsonStr(response));
}
}
} @Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Lists.newArrayList("*"));
config.setAllowCredentials(true);
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
} @Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo()).enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.vines.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("项目描述")
.description("基础服务项目描述")
.contact(new Contact("作者", "作者URL", "作者Email"))
.version("1.0")
.build();
}
} @Data
public class ResponseResult<T> {
private int code;
private String message;
private T data;
public static <T> ResponseResult<T> success(T data) {
ResponseResult<T> responseResult = new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
responseResult.setData(data);
return responseResult;
}
public static <T> ResponseResult<T> success() {
ResponseResult<T> responseResult = new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
return responseResult;
}
public static <T> ResponseResult failed(int code, String message) {
ResponseResult<T> responseResult = new ResponseResult<>();
responseResult.setCode(code);
responseResult.setMessage(message);
return responseResult;
}
public static boolean isSucceed(ResponseResult responseResult) {
return responseResult.getCode() == ResultCode.SUCCESS.getCode();
}
}Additional Tools
Recommendations for embedded versions of Redis, databases, Kafka, as well as libraries such as Hutool, MyBatis‑Plus, MapStruct, and Redisson are listed, with Maven coordinates where applicable.
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>Conclusion
The article reminds readers that beyond IDE setup, version compatibility and proper tooling are the real time‑consumers in real projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
