SpringBoot (2): Use of Controller and configuration files in SpringBoot

1. The use of Controller in SpringBoot

1.1, return to view

1.1.1 controller code

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("test")
public class TestController1 {
    
    
    @RequestMapping("test01")
    public String test01(Model model){
    
    
        model.addAttribute("msg","Hello World!!!");
        return "index";
    }
}

note:

  • Spring boot supports the thymeleaf template engine by default, so we need to introduce the following dependencies in the pom.xml file

      <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
    

1.1.2 Create index.html

We must first know the role of these file directories:
Insert picture description here

Create an index.html page in the templates directory under the root directory of the resources file

Insert picture description here

<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="${msg}"></p>
</body>
</html>

note:

  • You can’t use el expressions in html pages, so use them when getting objects in the model

    th:attribute="${msg}"

1.1.3 Open the webpage in the browser

Start BootStartApplication, enter
http://localhost:8080/test/test01 in the browser, you
Insert picture description here
can see that the page outputs Hello World!!!

1.2, return json data

1.2.1 Controller code

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller//可以更改为@RestController,此处更改后下面的ResponseBody就不用写了
@RequestMapping("test")
public class TestController1 {
    
    
    @ResponseBody//让返回值类型变为json格式
    @RequestMapping("test02")
    public String test02(){
    
    
        return "Hello SpringBoot!!!";
    }
}

1.2.2 Open webpage in browser

Start BootStartApplication, enter
http://localhost:8080/test/test02 in the browser, you
Insert picture description here
can see that the page outputs Hello SpringBoot!!!

1.3 Use of lombok plugin (recommended to master)

Good things, learn! ! ! !

1.3.1 Install the plug-in:

Reference
https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html

1.3.2. Add lombok dependency

Add lombok dependency in pom.xml file

Insert picture description here

 <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.16</version>
 </dependency>

1.3.3. Write Controller

Some students will ask, how do we return an entity class to the page? Come on the code

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller//可以更改为@RestController,此处更改后下面的ResponseBody就不用写了
@RequestMapping("test")
public class TestController1 {
    
    
    @Data
    @AllArgsConstructor
    class Person{
    
    
        private String name;
        private int age;
    }
    @RequestMapping("test03")
    @ResponseBody//让返回值类型变为json格式
    public Person test03(){
    
    
        return new Person("张三",18);
    }
}

Here we use lombok's annotations, which can automatically generate get and set methods by adding annotations, and can also generate construction methods. Is it very convenient? Of course, there are many other APIs that can be used:

Insert picture description here
Interested students can try it by themselves, and I won’t elaborate on it here.

1.3.4 Open the webpage in the browser

Start BootStartApplication, enter
http://localhost:8080/test/test03
Insert picture description here
page in the browser with data coming out

Second, the use of Controller in SpringBoot

2.1 Configuration file reading order

  • 1) Command line
  • 2) JNDI attributes in java:comp/env
  • 3) JVM system properties
  • 4) Operating system environment variables
  • 5) Random.* property generated by RandomValuePropertySource property class
  • 6) Application.properties (or yml) files outside the application
  • 7) The application.properties (or yml) file packaged in the application
  • 8) In the application @Configuration configuration class, the property file declared with @PropertySource annotation
  • 9) The default properties declared by SpringApplication.setDefaultProperties

2.1 application.propreties (yml) read configuration

2.1.1 The official default configuration

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

2.1.2 Custom configuration

E.g

student.name=zhangsan
student.age=15
student.sex=man

Insert picture description here
So, how do you use the parameters in the file?

Come on code

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
@ConfigurationProperties(prefix = "student")
@Data
public class TestController1 {
    
    
    private String name;
    private int age;
    private String sex;

    @RequestMapping("test04")
    @ResponseBody//让返回值类型变为json格式
    public String test04(){
    
    
        return "name="+name+" age="+age+" sex="+sex;
    }
}

In addition, you also need to add the configuration file scanning configuration to the startup file, otherwise the startup will be invalid

Insert picture description here

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan
public class BootStartApplication {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(BootStartApplication.class, args);
	}
}

Then, we can go to the browser to view it
Insert picture description here

2.1.3 yml file configuration

Entering Chinese in the ordinary application.properties file, the data is transmitted to the interface, there will be garbled characters, but the yml file will not cause such a problem, the configuration method of the yml file is slightly different from the configuration method of the properties file, but It is the same to use

2.1.3.1 yml file configuration

Insert picture description here

student:
name: Zhang San
age: 15
sex: male
address: Changsha, Hunan
des: my name is student. name, I am {student.name} this year, I am this yearstudent.name,I am now in {student.age} years old, from $ {student.address}

java file configuration:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
@ConfigurationProperties(prefix = "student")
public class TestController1 {
    
    
    @Value("${student.des}")
    private String des;

    @RequestMapping("test05")
    @ResponseBody//让返回值类型变为json格式
    public String test05(){
    
    
        return des;
    }
}

Finally, open the browser, enter http://localhost:8080/test/test05,
Insert picture description here
you can see that the data is transmitted, and there is no garbled in the Chinese format

So, another classmate asked, should I put all my configuration in a yml file, in fact, it is not, SpringBoot scans application.yml or application.properties files, but we can customize some other files, Start with application and end with -xxx.yml, for example:

Insert picture description here

student:
name: Zhang San
age: 15
sex: male
address: Changsha, Hunan
des: my name is student. name, I am {student.name} this year, I am this yearstudent.name,I am now in {student.age} years old, from $ {student.address}

Insert picture description here

spring:
profiles:
active: common

java code:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
@ConfigurationProperties(prefix = "student")
public class TestController1 {
    
    
    @Value("${student.des}")
    private String des;

    @RequestMapping("test06")
    @ResponseBody//让返回值类型变为json格式
    public String test06(){
    
    
        return des;
    }
}

Then enter http://localhost:8080/test/test06 in the browser
to see the result. At
Insert picture description here
this point, the use of Controller and configuration files in SpringBoot will be introduced.
Thank you for leaving a comment.

Guess you like

Origin blog.csdn.net/m0_50217781/article/details/112485241