1. Getting Started with SpringBoot
(1) The first Springboot case
1. Create a Springboot project and add dependencies.
2. Define the class, add methods and add annotations
3. Run the test.
pom.xml (automatically generated by the framework)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zengoo</groupId>
<artifactId>JavaEEpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JavaEEpro</name>
<description>JavaEEpro</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Control class Controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("Hello World!");
return "Hello World!";
}
}
start springboot
renderings
2. HTTP protocol
(1) Overview
1. Concept: HTTP (Hyper Text Transfer Protocol), the hypertext transfer protocol, stipulates the rules for transferring data between the browser and the server.
2. Features
(1) Based on TCP protocol: connection-oriented and secure
(2) Based on request-response: one request corresponds to one response
(3) HTTP is a stateless protocol: it has no memory for transaction processing. Each request-response is independent.
3. Advantages and disadvantages
- Advantages: fast.
- Disadvantage: Data cannot be shared between multiple requests.
2. Data header
(1) Data transmission status
Request URL: http://localhost:8080/hello
Request Method: GET
Status Code: 200
Remote Address: [::1]:8080
Referrer Policy: strict-origin-when-cross-origin
(2) Request header
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,
image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: Idea-215396a0=c0d39e88-d7fa-44d7-9967-81f5a14b6018
Host: localhost:8080
Sec-Ch-Ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML
, like Gecko) Chrome/114.0.0.0 Safari/537.36
(3) Response header
Connection: keep-alive
Content-Length: 12
Content-Type: text/html;charset=UTF-8
Date: Mon, 12 Jun 2023 06:30:01 GMT
Keep-Alive: timeout=60
(2) Request agreement
1. Request line: refers to the first line of request data (request method + resource path + protocol)
2. Request header: It means that the request data starting from the second line of the request data is stored in the form of [key: value].
describe | illustrate |
---|---|
Host | requested hostname |
User-Agent | browser version |
Accept | The browser receives the resource type |
Accept-Language | browser preference language |
Accept-Encoding | Compression types supported by browsers |
Content-Type | request body type |
Content-Length | Request body size (Byte) |
3. Request body: the area where the post request stores the requested data.
Console output after post request
4. Supplementary knowledge:
(1) GET request: the request data size is limited, and the range is the length of the string input in the address bar
(2) POST request: there is no limit to the request data, and the JSON format is used for transmission
(3) Response protocol
1. Response line: the first line of response data. (protocol + status code + description)
(1) status code
status code | illustrate |
---|---|
1xx | In Response - a temporary status code, indicating that data has been received, telling the client that the request should continue or ignore it if completed |
2xx | SUCCESS - Indicates that the request was received successfully and processing is complete |
3xx | Redirect - Redirect to other places, let the client re-initiate the request to complete the entire process |
4xx | Client error - processing errors occur, the responsibility lies with the client, such as requesting a resource that does not exist, the client is not authorized, access is prohibited, etc. |
5xx | Server Error - When an error occurs, the responsibility is on the server side, such as a program throwing an exception, etc. |
Common Status Codes
status code | English description | explain |
---|---|---|
200 | OK | client request successful |
302 | Found | Indicates that the requested resource has been given the URL by the Location response header |
304 | Not Modified | Indicates that there is already data in the cache and can be obtained directly |
400 | Bad Request | The request has syntax errors and the server cannot parse it |
403 | Forbidden | Request received, but service refused |
404 | Not Found | The requested resource does not exist. Generally, the URL is entered incorrectly or the website resource is deleted. |
405 | Method Not Allowed | The request method is wrong. A resource that should use the GET method uses the POST method |
428 | Procondition Required | The server requires requests with special request headers to access |
429 | Too Many Request | Too many requests error. Use with the Retry-After (use after interval) response header |
431 | Request Header Fields Too Large | The request header is too large. The server could not accept it. |
500 | Internal Server Error | Unpredictable exception occurred on the server |
503 | Server Unavailable | server is not ready |
Response code collection: https://cloud.tencent.com/developer/chapter/13553
2. Response header: the description of the corresponding data terminal starting from the second line of the corresponding data, stored in the form of [key: value]
describe | illustrate |
---|---|
Content-Type | Response body type |
Content-Length | Response body size (Byte) |
Content-Encoding | Response Compression Algorithm |
Cache-Control | Client cache configuration |
Set-Cookie | Tell the browser to set the cookie for the domain of the current page |
3. Response body: respond to specific data
(4) Protocol analysis
1. Protocol analysis refers to the process of customizing a web server to receive the request sent by the client and return the response body using ServerSocket.
2. Servers with protocol analysis: Tomcat, Jetty, WebLogic, WebSphere, etc...
3. Tomcat server
(1) Concept: Tomcat is a core project of the Apache Software Foundation. It is an open source and free lightweight web server that supports a small number of JavaEE specifications for Servlet/JSP.
(2) Official website: https://tomcat.apache.org/
(3) Basic use
- Download Tomcat 8.0
- Installation: Tomcat can be decompressed
- Uninstall: delete the directory directly
- Start: Click bin\startup.bat
- Console Chinese garbled problem: Modify conf/logging.properties
- Console Chinese garbled problem: Modify conf/logging.properties
- closure
- Close the windows window directly: force close
- bin\shutdown.bat: graceful shutdown
- Ctrl+C: normal shutdown
- Modify Tomcat port number
- Deployment project: package the project and place it in the webapps directory
Open renderings
(4) Tomcat file directory table
folder name | illustrate |
---|---|
bin | executable directory |
conf | configuration file directory |
lib | Dependency file directory |
logs | run log file directory |
temp | temporary file directory |
webapps | App Release Directory |
work | Work list |
(5) Common problems of Tomcat startup
common problem | Approach |
---|---|
The startup window flashes by | Check whether the JAVA_HOME environment variable is correct |
Port number conflict | Find the corresponding program and kill it or modify the Tomcat port number ( conf/server.xml ) |
4. Embedded Tomcat
Spring officially gave us a website that can create a Maven project skeleton https://start.spring.io/
Click Genarate to get the compressed package
Click Explore to view the skeleton content in the web page