SpringBoot初始化HelloWorld maven版

99 阅读1分钟

环境约束

jdk1.8;maven3.3 ; IntelliJ IDEA 2023; SpringBoot 2.4

创建项目

maven.png

输入项目名

配置maven 依赖 pom.xml

注意依赖这个 spring-boot-starter-web

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/>
    </parent>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <groupId>org.example</groupId>
    <artifactId>imgServer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </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>

        </plugins>
    </build>
</project>

写启动类

这里有个注意点,要创建包,

package net.img;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"net.img"})
public class ImgServer implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ImgServer.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(" 已启动");
    }
}

写个controller,输出helloWorld

package net.img;

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

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World!";
    }
}

启动项目,看控制台输出如下

启动类上直接 Run ImgServer 即可

run.png

启动项目

控制台显示如下日志,说明项目已经启动

2024-01-09 23:20:41.456  INFO 98403 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-01-09 23:20:41.457  INFO 98403 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2024-01-09 23:20:41.522  INFO 98403 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-01-09 23:20:41.522  INFO 98403 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1102 ms
2024-01-09 23:20:41.665  INFO 98403 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2024-01-09 23:20:41.842  INFO 98403 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-01-09 23:20:41.852  INFO 98403 --- [           main] net.img.ImgServer                        : Started ImgServer in 1.956 seconds (JVM running for 2.607)
 已启动

浏览器输入URL,查看返回值

hello.png

helloWorld 访问