Java Web Template Project

Beck Moulton
5 min readSep 30, 2022

--

This article will introduce how to build a commonly used Java Web template project and publish the template project to the local repository. The advantages of using template projects are as follows.

  1. Create projects quickly and avoid duplication of effort
  2. The version used in the enterprise is stable and controllable

What is it?

This section will discuss a Java Web project template project should have what components, what package path. First of all, what components should be discussed, as a Java Web project at least need to have a Web development library, common Web development libraries SpringBoot, Quarkus, Micronaut, etc., according to the current trend SpringBoot will be the first choice, determine the Web development library also need to further confirm other components, in the Web development process usually also need a database as data persistence support, common databases are MySQL, Oracle, PostgreSQL, etc., here to MySQL as the first choice. After confirming the completion of the database type, the next step is to confirm the ORM framework. This template project adopts MybatisPlus. In addition, because the project is a Web project, Swagger is introduced in order to facilitate the viewing of the interface doc. To sum up, the dependency information required by the template project is as follows.

  1. Spring Boot
  2. MySQL Driver
  3. MybatisPlus
  4. Swagger
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.kop</groupId>
<artifactId>web-template-project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis-plus-version>3.5.2</mybatis-plus-version>
<springfox-boot-starter-version>3.0.0</springfox-boot-starter-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${springfox-boot-starter-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

The related components have been introduced. Next, build the related packages. The specific packages include the following contents

  1. com.github.kop.template: The root package path, where the startup class is stored
  2. com.github.kop.template.api: for storage API interface
  3. com.github.kop.template.module: Used to store Java classes
  4. com.github.kop.template.module.biz: Used to store project business classes
  5. com.github.kop.template.module.entity: Used to store database entity classes
  6. com.github.kop.template.module.enums: for storing enumeration classes
  7. com.github.kop.template.module.ex: Used to store exception classes
  8. com.github.kop.template.module.req: Used to store request parameters
  9. com.github.kop.template.module.res: for storing response parameters
  10. com.github.kop.template.repo: Classes for storing interactions with the database
  11. com.github.kop.template.repo.mapper: Mapper interface for storing with Mybatis
  12. com.github.kop.template.service: Used to store specific business processing classes
  13. com.github.kop.template.utils: Used to store tool classes
  14. com.github.kop.template.config: Used to store configuration classes

After the above types of packages are created, you need to fill in some key contents. The first is to add the global common response object RespVO. The specific definition code is as follows.

@ApiModel(value = "", description = "")
@Data
public class RespVO<T> {
private int code;
private String msg;
private T data;
public static <T> RespVO<T> error() {
RespVO<T> tRespVO = new RespVO<>();
tRespVO.setCode(4001);
tRespVO.setMsg("处理异常");
return tRespVO;
}
public static <T> RespVO<T> error(AppHttpCodeEnum appHttpCodeEnum) {
return error(appHttpCodeEnum.getMsg());
}
public static <T> RespVO<T> error(String msg) {
RespVO<T> tRespVO = new RespVO<>();
tRespVO.setCode(4001);
tRespVO.setMsg(msg);
return tRespVO;
}
public static <T> RespVO<T> error(Integer code, String msg) {
RespVO<T> tRespVO = new RespVO<>();
tRespVO.setCode(code);
tRespVO.setMsg(msg);
return tRespVO;
}
public static <T> RespVO<T> success() {
RespVO<T> tRespVO = new RespVO<>();
tRespVO.setCode(2001);
tRespVO.setMsg("handler success");
return tRespVO;
}
public static <T> RespVO<T> success(T data) {
RespVO<T> tRespVO = new RespVO<>();
tRespVO.setCode(2001);
tRespVO.setMsg("handler success");
tRespVO.setData(data);
return tRespVO;
}
}

Secondly, the response enumeration of the global common condition, the specific definition code is as follows

@Getter
public enum AppHttpCodeEnum {
UPDATE_ERROR(3003, "update error"),
UPDATE_ID_ERROR(3004, "id require"),
;
int code;
String msg;
AppHttpCodeEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}

Next, add the global exception class, the specific code is as follows.

public class NoceException extends RuntimeException {
private static final long serialVersionUID = 1L;
private AppHttpCodeEnum enums;
private String msg;
private int code = 3500;
public NoceException(String msg) {
super(msg);
this.msg = msg;
}
public NoceException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public NoceException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public NoceException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public NoceException(AppHttpCodeEnum enums, String message) {
super(message);
this.enums = enums;
this.code = enums.getCode();
}
public NoceException(AppHttpCodeEnum enums) { this.enums = enums;
}
public AppHttpCodeEnum getEnums() {
return enums;
}
public void setEnums(AppHttpCodeEnum enums) {
this.enums = enums;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}

With the above preparation code, write the following global exception interceptor, the specific code is as follows.

@RestControllerAdvice
@Slf4j
@RestController
public class ExceptionCatch {
public static final String ERROR_MSG = "error_msg"; @ExceptionHandler(Exception.class)
public RespVO<String> exception(Exception e) {
log.error(e.getMessage());
e.printStackTrace();
return RespVO.error(e.getMessage());
}
@ExceptionHandler(NoceException.class)
public RespVO<String> leadExceptionHandler(NoceException nsException) {
nsException.printStackTrace();
return RespVO.error(nsException.getCode(), nsException.getMessage());
}
}

Maven Skeleton Project Creation

This section will introduce how to create a Maven skeleton project, first enter the project directory made above, and then execute the mvn archetype: create-from-project command. After the execution is completed, you will see something like the following in the console to indicate that the creation is successful

Archetype project created in /Users/zhangsan/git_repo/kop/web-template-project/target/generated-sources/archetype

Next, you need to enter the file location output in the above console, cd /Users/zhangsan/git_repo/kop/web-template-project/target/generated-sources/archetype and then execute the mvn install command to complete the installation. After the installation is complete, you need to import the template information into the Maven repository for use, and execute the mvn archetype: crawl command to complete the import work.

Maven skeleton project use

This section will introduce how to use the Maven skeleton project to create a project. First execute the mvn archetype: generate command. At this time, the following content will be output on the console (the following content varies depending on the personal environment)

Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
11: local -> io.quarkus:quarkus-funqy-amazon-lambda-archetype (${artifactId})
12: local -> io.quarkus:quarkus-amazon-lambda-http-archetype (${artifactId})
13: local -> io.quarkus:quarkus-azure-functions-http-archetype (${artifactId})
14: local -> io.quarkus:quarkus-amazon-lambda-archetype (${artifactId})
15: local -> io.quarkus:quarkus-amazon-lambda-rest-archetype (${artifactId})
16: local -> com.github.kop:web-template-project-archetype (web-template-project)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7:

In this example, the template project is com.github.kop: web-template-project-archetype (web-template-project) The specific number is 16, enter 16 and press the Enter key, according to the command line prompts to enter the relevant content to complete the project creation.

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet