Spring Boot API Project Seed – A Backend Development Starter Kit with Code Samples
This article introduces a Spring Boot API project seed that combines MyBatis, a generic Mapper plugin, and PageHelper, offering ready‑made project structure, unified response handling, exception processing, code generation utilities, signature authentication, and integration guides to accelerate backend development.
Recently the author built several small‑to‑medium API projects using Spring Boot together with MyBatis, a generic Mapper plugin, and the PageHelper pagination plugin, and found the combination very comfortable for development. To avoid repeating the same setup steps, a reusable seed project was created and open‑sourced on GitHub.
The seed provides a best‑practice project layout, a streamlined POM, and common utilities such as a unified API response wrapper, global exception handling, basic CRUD service abstractions, and a simple request‑signature interceptor.
Key code examples include:
/**
* Unified API response wrapper
*/
public class Result {
private int code;
private String message;
private Object data;
public Result setCode(ResultCode resultCode) {
this.code = resultCode.code;
return this;
}
// getters and setters omitted
} /**
* Response code enum, mirroring HTTP semantics
*/
public enum ResultCode {
SUCCESS(200),
FAIL(400),
UNAUTHORIZED(401),
NOT_FOUND(404),
INTERNAL_SERVER_ERROR(500);
public int code;
ResultCode(int code) { this.code = code; }
}
public class ResultGenerator {
private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
public static Result genSuccessResult() {
return new Result().setCode(ResultCode.SUCCESS).setMessage(DEFAULT_SUCCESS_MESSAGE);
}
public static Result genSuccessResult(Object data) {
return new Result().setCode(ResultCode.SUCCESS).setMessage(DEFAULT_SUCCESS_MESSAGE).setData(data);
}
public static Result genFailResult(String message) {
return new Result().setCode(ResultCode.FAIL).setMessage(message);
}
} public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HandlerExceptionResolver() {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
Result result = new Result();
if (e instanceof ServiceException) {
result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
logger.info(e.getMessage());
} else if (e instanceof NoHandlerFoundException) {
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在");
} else if (e instanceof ServletException) {
result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
} else {
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
logger.error(e.getMessage(), e);
}
responseResult(response, result);
return new ModelAndView();
}
});
} public interface Service<T> {
void save(T model);
void save(List<T> models);
void deleteById(Integer id);
void deleteByIds(String ids);
void update(T model);
T findById(Integer id);
T findBy(String fieldName, Object value) throws TooManyResultsException;
List<T> findByIds(String ids);
List<T> findByCondition(Condition condition);
List<T> findAll();
} public abstract class CodeGenerator {
public static void main(String[] args) { genCode("输入表名"); }
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
genModelAndMapper(tableName);
genService(tableName);
genController(tableName);
}
}
// other generation methods omitted
} private boolean validateSign(HttpServletRequest request) {
String requestSign = request.getParameter("sign");
if (StringUtils.isEmpty(requestSign)) return false;
List<String> keys = new ArrayList<>(request.getParameterMap().keySet());
keys.remove("sign");
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (String key : keys) {
sb.append(key).append("=").append(request.getParameter(key)).append("&");
}
String linkString = sb.substring(0, sb.length() - 1);
String secret = "Potato";
String sign = DigestUtils.md5Hex(linkString + secret);
return StringUtils.equals(sign, requestSign);
}In addition, the seed integrates Druid for database connection pooling, Fastjson for high‑performance JSON serialization, and provides a simple code generator that can produce Model, Mapper, Service, ServiceImpl, and Controller classes based on table names.
All configuration links (Spring Boot, MyBatis, Mapper plugin, PageHelper, Druid, Fastjson) are listed, and the project URL ( https://github.com/lihengming/spring-boot-api-project-seed) is shared for cloning and further customization.
Readers are encouraged to submit issues or pull requests to improve the seed, and the article also contains promotional notes inviting readers to follow the author’s WeChat public account for additional resources.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
