Springboot搭建一个最简单的web服务

136 阅读1分钟

环境:Jdk17+Springboot3.2.5+Maven3.6.2

IDEA->New Project新建一个Maven工程,pom.xml中加入依赖:

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

Person类,String nameint age两个属性。

SpringbootStartApplication

package com.zhidian;

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

@SpringBootApplication
public class SpringbootStartApplication {

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

HelloController

package com.zhidian;

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

@RestController
public class HelloController {

    @GetMapping("/hello")
    public Person welcome(){
        return new Person("White", 23);
    }
}

我们用curl命令在终端访问看一下效果:
截屏2024-07-05 00.04.35.png