SpringBoot项目创建 helloworld

135 阅读1分钟

环境约束

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

创建项目

输入项目名称

book.jpg

Idea 创建项目 注意选择gradle 项目

配置gradle 依赖 build.gradle

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

buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }
    }

    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.4.1'
    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'idea'

group 'org.example'
version '1.0-SNAPSHOT'



repositories {
    mavenLocal()

    maven {
        url 'https://maven.aliyun.com/repository/public/'
    }
    maven {
        url 'https://maven.aliyun.com/repository/central'
    }
    maven {
        url 'https://maven.aliyun.com/repository/spring-plugin'
    }
    maven {
        url 'https://maven.aliyun.com/repository/gradle-plugin'
    }
    maven {
        url 'https://maven.aliyun.com/repository/jcenter'
    }

    mavenCentral()

}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation('org.springframework.boot:spring-boot-starter-web')
}

test {
    useJUnitPlatform()
}

写启动类

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

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 即可

启动项目

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

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,查看返回值

helle.jpg

helloWorld 访问