This is the 12th day of my participation in the August Challenge
1. Spring isCreated to address the complexities of enterprise application developmentTo simplify development.
2. In general, the SpringBoot project structure is similar to the following:
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- controller | +- CustomerController.java | +- config | +- swagerConfig.java |Copy the code
Application.java
Is the project startup class- Domain directories are mainly used for Entity and Data access layer.
- The Service layer is mainly business class code
- Controller is responsible for page access control
- The config directory mainly houses configuration classes
3. @RequestMapping
Use @requestMapping to map urls, usually at class definitions and method definitions.
Class definition: specifies the initial request mapping, relative to the web application root directory;
Method definition: Further subdivides the request map relative to the URL at the class definition. If the annotation is not used at the class definition, the URL of the method tag is relative to the root directory;
@requestMapping (value=”/example”) = @requestMapping (“/example”);
4. Discussion on springboot principle
(1) Automatic configuration
pom.xml
- Spring-boot-dependencies: The core dependencies are in the parent project
- We do not need to specify versions when writing or importing springBoot dependencies, just because of the repository
starter
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Copy the code
-
The launcher is basically the boot scenario of SpringBoot;
-
For example, spring-boot-starter-web automatically imports all the dependencies of the Web environment.
-
Springboot will turn all of the functional scenarios into initiators;
-
If you want to use something, you just need to find the corresponding initiator, right
The main program
// @springBootApplication: Marks this class as a SpringBoot application
@SpringBootApplication
public class SpringbootO1HellowordApplication {
public static void main(String[] args) {
// Start the SpringBoot applicationSpringApplication.run(SpringbootO1HellowordApplication.class, args); }}Copy the code
- Comments:
@SpringBootConfiguration / / springboot configuration
@Configuration / / the spring configuration classes
@Component // This is a Spring component
@EnableAutoConfiguration // Automatic configuration
@AutoConfigurationPackage // Automatic configuration package
@Import({Registrar.class})// Import the selector
@Import({AutoConfigurationImportSelector.class})// Automatic configuration import selection
Copy the code
5. JSR-303
Jsr-303 is a sub-specification in JAVA EE 6 called Bean Validation, Hibernate Validator is a reference implementation for Bean Validation. Hibernate Validator provides all of the built-in implementations of the JSR 303 specification. There are additional constraints.
The constraint is built into the Bean Validation
! [Screengrab 2021-07-14 3.10.35 PM](SpringBoot Beginner. Assets/Screengrab 2021-07-14 3.10.35.png)
Hibernate Validator has additional constraints
! [Screengrab 2021-07-14 3.10.57 PM](SpringBoot Beginner. Assets/Screengrab 2021-07-14 3.10.57.png)
From www.jianshu.com/p/554533f88…
6. Yaml configuration file location and priority
== Priority order: ==
- file:./config/
- file:./
- classpath:/config/
- classpath:/
! [Screengrab 2021-07-14 3.24.10 PM](SpringBoot Beginner. Assets/Screengrab 2021-07-14 3.24.10.png)
7. The difference between @Controller and @restController
The @restController annotation is equivalent to @responseBody + @Controller put together.
The return statement under @restController is returned as a string.
8. @GetMapping
The @getMapping annotation maps HTTP GET requests to specific handler methods. It is a composite annotation that is used as a shortcut to @requestMapping (method = requestMethod.get).
- GET – Requests data from the specified resource.
- POST – Submits data to be processed to the specified resource.
9. Handle static resources
(1) In SpringBoot, we can handle static resources in the following ways
- webjars localhost:8080/webjars/
- Public, static, /** ,resources. Localhost:8080/
(2) Priority: Resources >static(default) > public