Spring Boot教程(2) – 运行第一个项目

上篇文章写了用四种方式创建SpringBoot项目,面就看看项目能不能跑起来,我们通过IDE和命令行,直观地理解框架的作用。

先来一张IntelliJ IDEA的截图,为项目刚创建时候的状态。左侧为项目目录区,右侧为代码区。

下面来看看用IntelliJ IDEA怎么跑:

当你创建好项目后,目录中只有一个DemoApplication.java,这个类是整个程序的入口。当然类的名字可能不一样,根据你写的项目名称,类名也不一样,类上总有一个 @SpringBootApplication 注解,这个注解的作用我们以后再详细说明。DemoApplication里有一个main方法,此方法可以作为程序的入口执行,在打开项目之后,IDEA会自动生成一个运行设置(Run Configuration),把DemoApplication作为程序运行的起点。

如果你迫不及待想跑跑看看,可以点击上面的“Run”按钮,绿色三角形那个。两秒之后,程序就跑起来了,下面是运行时输出的信息:

这里面是包含了很多东西的,比如Spring Boot版本是2.1.8,内置Tomcat的版本是9.0.24,Web服务器的端口是8080,进程ID为2512等等。这会儿你该疑惑了,我的程序都跑起来了,怎么没有网页跳出来呢?去哪里看呢?你可以打开 localhost:8080 看看:

出错了。因为你的程序虽然跑起来了,然是并不能处理任何请求。你需要在项目目录中新建一个MyController的类(类名是啥无所谓),内容如下:

package com.example.demo;

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

@Controller
@RequestMapping("test")
public class MyController {

    @RequestMapping("index")
    @ResponseBody
    public String index(){
        return "SpringBoot.....index";
    }
}

MyController  有一个 @Controller 注解,这个注解告诉框架这个类是用来处理Web请求的,@RequestMapping("test"),告诉请求路径,@ResponseBody 注解表示返回请求内容,如果return是HTML地址就不需要写。会直接跳转到HTML地址。

最后pom文件的配置:

<?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.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </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>
发布了210 篇原创文章 · 获赞 1042 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/onceing/article/details/101367721
今日推荐