Last time I said SpringBoot is actually a CI tool, how to experience the role of CI is continuous integration, it can integrate various tools, here is about the template integration engine and Swagger.
(I) Spring Boot integrated template engine to realize Web applications
- Static resource access
Static resource
Js, CSS, HTML, images, audio and video
Static resource path
A path that can be directly accessed by the system. All files in the path can be read by users. Spring Boot provides static resource directories by default. The directories must be placed in classpath and comply with the following rules: /static, /public, /resources, / meta-INF /resources
Create the static directory under the classpath and add a picture a.png
After joining, then don’t need to restart the direct access: http://localhost:8888/a.jpg
Properties to modify the default static resource directory
spring.resources.static-locations
Copy the code
(2) Integrated template engine
Spring Boot strongly recommends using a template engine to render HTML pages instead of using JSPS, which will not allow you to implement many of Spring Boot’s features. Thymeleaf(Recommended by Spring Boot), FreeMarker.
- Thymeleaf
Spring boot default template configuration path is: the SRC/main/resources/templates. Of course, you can also modify this path through the properties of the configuration file, which is explained in detail in the configuration file last time.
Integrate the Thymeleaf step
1. Modify pom. XML to add the following dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Copy the code
2. Writing the Controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/ * * *@program: springboot3d
* @description: ${description}
* @author: LiMing
* @create: the 2019-06-09 09:15 * * /
@Controller
public class SampleController {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(ModelMap map) {
// Set the properties
map.addAttribute("name"."idig8");
// testThymeleaf: is the name of the template file
/ / SRC/main/resources/templates/testThymeleaf. HTML
return "testThymeleaf"; }}Copy the code
3. Under the SRC/main/resources/build templates/testThymeleaf. HTML
<! DOCTYPEhtml>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
<meta charset="UTF-8" />
<title>testThymeleaf</title>
</head>
<body>
<h1 th:text="${name}">idig88</h1>
</body>
</html>
Copy the code
4. Run the spring boot, the browser input: http://localhost:8888/testThymeleaf
- FreeMarker
1. Modify pom. XML to add dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Copy the code
2.Controller
@RequestMapping("/testFreemarker")
public String testFreemarker(Map<String,String> map) {
map.put("name"."Zhang");
return "hello"; / / the default for the SRC/main/resources/templates/hello.html FLT
}
Copy the code
Hello. FLT, SRC \main\resources\templates
<html>
<body>
hello, ${name}
</body>
</html>
Copy the code
4. Run the spring boot the main, the browser enter the following address: http://localhost:8881/testFreemarker
(2) Integrate Swagger2 to build RESTful API documents
- Swagger2
1. Powerful RESTful API documents are automatically generated along with the project to reduce workload. 2. Page test functionality to debug each RESTful API
- Integrate Swagger2 steps
1. Modify pom. XML to add Swagger2 dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Copy the code
2. Create the Swagger2 configuration class
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/ * * *@program: springboot3d
* @description: ${description}
* @author: LiMing
* @create: the 2019-06-09 10:20 * * /
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(a) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.idig8.springboot"))// Specify the annotation under the scan package
.paths(PathSelectors.any())
.build();
}
// Create the basic information of the API
private ApiInfo apiInfo(a) {
return new ApiInfoBuilder()
.title("Integrate Swagger2 to build RESTful APIs")
.description("Integrate Swagger2 to build RESTful APIs")
.termsOfServiceUrl("https://www.idig8.com")
.contact("Welcome to: Too many programming holes")
.version("1.0.0") .build(); }}Copy the code
2. Create Controller: SwaggerController.java
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/ * * *@program: springboot3d
* @description: ${description}
* @author: LiMing
* @create: the 2019-06-09 10:22 * * /
@RestController
@RequestMapping(value="/swagger")
public class SwaggerController {
@apiOperation (value=" get user information ", notes=" get user details by ID ")
@APIIMPLICITParam (name="id", value=" user ID", Required =true, dataType="String")
@RequestMapping(value="/{id}", method= RequestMethod.GET)
public Map<String,String> getInfo(@PathVariable String id) {
Map<String ,String> map = new HashMap<String, String>();
map.put("name"."Zhang");
map.put("age"."34");
returnmap; }}Copy the code
4. Start the Spring boot, access to Swagger UI: http://localhost:8881/swagger-ui.html
PS: Today we introduced the simple template engine and Swagger2. Start with the ability to use it, specific details or see the official API more detailed, here is just from the beginning.