SpringBoot usage entry and case implementation

1. Add dependencies in pom.xml

Proceed as follows:

  1. Add springboot's parent dependency
  2. Add the starter-web dependency of springboot. There are also many starter starters for other scenarios
  3. Modify the version number of mysql
  4. Add springboot packaging plugin

The content is as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hh</groupId>
    <artifactId>springbootTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

    <properties>
        <!-- 修改mysql的版本号 -->
        <mysql.version>8.0.25</mysql.version>
    </properties>

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

    <build>
        <plugins>
		<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>${shade.plugin.version}</version>
				<dependencies>
					<dependency>
						<groupId>org.springframework.boot</groupId>
						<artifactId>spring-boot-maven-plugin</artifactId>
						<version>${spring.boot.version}</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<artifactSet>
								<excludes>
									<exclude></exclude>
								</excludes>
							</artifactSet>
							<filters>
								<filter>
									<artifact>*:*</artifact>
									<excludes>
										<exclude>META-INF/*.SF</exclude>
										<exclude>META-INF/*.RSA</exclude>
										<exclude>META-INF/*.DSA</exclude>
										<!-- 解决java -classpath ... class运行错误:ERROR StatusLogger Unrecognized format specifier -->
										<exclude>**/Log4j2Plugins.dat</exclude>
									</excludes>
								</filter>
							</filters>
							<transformers>
								<transformer
										implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
									<resource>reference.conf</resource>
								</transformer>
								<transformer
										implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
									<resource>META-INF/spring.factories</resource>
								</transformer>
								<transformer
										implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass></mainClass>
									<!-- 解决: WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance -->
									<manifestEntries>
										<Multi-Release>true</Multi-Release>
									</manifestEntries>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
        </plugins>
    </build>

</project>

2. Edit resources/application.properties

The name of the configuration file is agreed upon. Modify the port number of tomcat as follows:

# 修改tomcat的端口号
server.port = 8088

The value of the configuration file will be bound to the corresponding class, and the class will create an object in the IOC container

It is also possible to define a configuration file application.yaml. It will be configured and merged with application.properties, but application.properties has a higher priority

3. Write the main program of springboot

Notes:

  • @SpringBootApplication: Indicates that this is a springboot application. All other folders and files in the same directory are automatically scanned

As follows:

package com.hh.springbootTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// springboot的程序入口,表明这是一个springboot应用
// 默认会自动扫描同一目录下的其它所有文件夹和文件
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {

        SpringApplication.run(MyApplication.class, args);

    }
}

4. Write the Controller program

Notes:

  • @Controller: indicates that this is a controller
  • @ResponseBody: Indicates that requesting the address will return data
  • @RequestMapping: Indicates the specific address of the request
  • @RestController: Equivalent to @ResponseBody + @Controller

As follows:

package com.hh.springbootTest.myController;

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

// @ResponseBody       // 也可以放在函数上面,单独修饰函数
// @Controller

@RestController     // 等同于:@ResponseBody + @Controller
public class HelloController {

    @RequestMapping("/hello")
    public String helloHandle() {
        return "hello springboot";
    }

}

5. IDEA local test

Run the MyApplication.java program directly, and then use a browser to visit http://localhost:8088/hello. The displayed effect is as follows:

IDEA local test

6. Package and upload to the server to run

Use mvn clean packagethe command to package, then upload the jar package to the server, and use the java command java -jar springbootTest-1.0-SNAPSHOT.jarto start the application in the foreground

Guess you like

Origin blog.csdn.net/yy8623977/article/details/127632747