Morning:
About the role of tags: declare the scope of jar
junit's dependency, if only used in testing, is not used in the main program
Add < scope > test < / scope >
Scope of maven dependency:
The previous Maven command:
mvn install: install to the local warehouse (previously set the location to change the. M2 path)
mvn deploy: deploy to local server (private server)
Export source code:
There are all maven plug-ins in the plugin of maven's official website, which can
View and find the appropriate plug-in
maven project package source code plug-in:
When you arrive at the company, you need to use your own private server (the company code must be private)
afternoon:
springboot learning
springboot
sringboot is a tool for users to quickly develop spring applications,
Quick reason: springboot has built-in a large number of initialization codes. Developers only need to supplement their own business codes to complete the development quickly
For web projects, tomcat server is built in, which can run quickly
Four functions of springboot
Quick run: automatically run projects
Auto configuration: automatically shield code that does not need to be executed
Auto dependency: automatically import related dependencies
Unified monitoring:
Spring boot core concepts
Startup class: the execution entry of the springboot project program
starter: module division of spring boot extension function
Unified configuration: application properties/yml
How to create a springboot project using maven
Spring official website, spring IO view springboot documentation
You can view all the dependencies of springboot in the official document 4.1.5
Specific steps:
1. Dependencies for importing springboot:
- Set the parent project of the current project as the springboot project:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> </parent>
- Import dependent
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
- Create a unified profile
- Create a startup class (this is created under the path of java, as shown in the following figure)
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }
The following figure shows the POM of a newly created maven project Display of XML file:
Configuration file (modify the port of the springboot Web project)
Some key points of yml file and properties file:
The second row of the advantages in the figure above says that the storage size and properties are much smaller, especially when there are multiple identical prefixes
Modify the port writing method in the properties file:
server.port=9090
In yml:
server: port: 9090
It can be noted that when multiple configurations in the yml file have the same prefix, it can be omitted to reduce the size of the file (compared with the properties file)
Modify the application server of the Springboot Web project
- Check the default tomcat server:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
- To import a new application server:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
The overall purpose of the above is to use jetty's web application server instead of the default tomcat server
Accessing static resources in a springboot Web project
- Teacher system notes:
- My notes:
springboot officially recommends that static resources be placed in one of the following directories:
"classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/","classpath:/public/"
The class path path represents the resources path
How to return json data in springboot web project [proficient]
json object:
json array (an array of json objects):
Interface requirements:
Provide an interface to return user information (json format)
lombok dependency reduces our workload (there is no need to write get and set methods):
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
The implementation based on @ Controller means that this method is right
@RestController is a composite annotation that integrates the @ Controller and @ ResponseBody annotations
springboot unit test [proficient]
- Dependencies for importing unit tests
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
- Write unit tests
- @SpringBootTest indicates that the current class is a unit test of a SpringBoot project
- classes specifies the startup class. The package name of the current test class is the same as that of the startup class. Configuration can be omitted
@SpringBootTest(classes = App.class) public class AppTest { @Test public void test(){ System.out.println("hello"); } }
- @SpringBootTest indicates that the current class is a unit test of a SpringBoot project
Note:
The path (package name) in the test unit test class should be the same as the java above, otherwise an error may be reported (as shown in the figure below):
Packaging of springboot Web project
The web project is packaged into a jar package, which can be executed. The jar package can be run directly through commands
Packaged plug-ins:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
The jar package already includes tomcat