SpringBoot - 2. Introductory case

content

1. Maven settings

2、HelloWord

2.1. Create a Maven project and introduce dependencies

2.2, create the main program

2.3. Writing business

2.4. Test

2.5. Simplified configuration

2.6. Simplified deployment


1. Maven settings

Configure or modify in conf/settings.xml

<mirrors>
    <!-- 配置具体的仓库的下载镜像 --> 
	<mirror> 
		<!-- 此镜像的唯一标识符,用来区分不同的mirror元素 --> 
		<id>nexus-aliyun</id> 
		<!-- 对哪种仓库进行镜像,简单说就是替代哪个仓库 --> 
		<mirrorOf>central</mirrorOf>
		<!-- 镜像名称 --> 
		<name>Nexus aliyun</name>
		<!-- 镜像URL --> 
		<url>https://maven.aliyun.com/repository/public</url>
	</mirror>
</mirrors>

<profiles>
	<profile>
     <id>jdk-8</id>
     <activation>
         <activeByDefault>true</activeByDefault>
         <jdk>8</jdk>
     </activation>
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target> 
		 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
     </properties> 
	</profile>
  </profiles>

2、HelloWord

Requirements: Browse sends /hello request, responds Hello, Spring Boot 2

2.1. Create a Maven project and introduce dependencies

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

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

2.2, create the main program

src/main/java/com/zyj/boot/MainApplication.java 

@SpringBootApplication  // 标识为一个SpringBoot应用,被标识的类称为主程序类
public class MainApplication {

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

}

2.3. Writing business

src/main/java/com/zyj/boot/controller/HelloController.java

/**
 * @ResponseBody 注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,
 * 写入到response对象的body区,而不是跳转到页面,通常用来返回JSON数据或者是XML
 */
//@ResponseBody
//@Controller
@RestController  // @RestController可以代替@Controller和@ResponseBody
public class HelloController {

    @RequestMapping("/hello")
    public String handler01(){
        return "hello,Spring Boot 2!";
    }

}

2.4. Test

Run the main method of the main program MainApplication directly, and then open http://localhost:8080/hello in the browser  , then the page will output hello, Spring Boot 2!

2.5. Simplified configuration

Create application.properties in src/main/resources, you can do some configuration in it, such as

#修改运行时的端口号
server.port=8888

Other configurations can be viewed on the official website

 2.6. Simplified deployment

Configure in pom.xml, type the project into a jar package, and execute it directly on the target server.

Note: If it becomes popular after being copied from the official website, you need to add the version and be consistent with the parent

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

After configuration, execute the Maven command clean - package

Enter the directory corresponding to the target, open cmd, and execute the following command

java -jar boot-01-helloworld-1.0-SNAPSHOT.jar

Then you can access the hello main program in the browser

Note: If you can't start it, you can try to cancel the quick edit mode of cmd

Guess you like

Origin blog.csdn.net/Mr_zhangyj/article/details/123848515