沙滩星空的博客沙滩星空的博客

JAVA使用SpringBoot创建Web项目

创建 spring boot 项目骨架

  1. 进入 https://start.spring.io 网页在线创建Web项目
  2. 工程类型选择 Maven Project, 语言:Java, JAVA版本:8
  3. 点击右上角选择依赖项。可输入 Web 选择 Spring Web
  4. 下载zip文件,并解压到本地电脑。

修改代码

  1. Before
package my.whq.hellosp;

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

@SpringBootApplication
public class HellospApplication {

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

}
  1. After
package my.whq.hellosp;

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

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HellospApplication {

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

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
    return String.format("Hello %s!", name);
    }

}

添加依赖

前面网页在线创建Web项目时,如忘记选择 Spring Web 依赖,可接下来手动修改 pom.xml 文件:

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

改为:

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

运行

  • Linux/MacOS: ./mvnw spring-boot:run
  • Windows: mvnw spring-boot:run

浏览器输入链接地址 http://localhost:8080/hello 即可访问。


SpringBoot 官方文档中文版 - 1. 入门指南 https://www.cnblogs.com/youcoding/p/15047719.html
https://spring.io/projects/spring-boot
https://spring.io/quickstart

未经允许不得转载:沙滩星空的博客 » JAVA使用SpringBoot创建Web项目

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址