Spring Boot RESTful demo

前言

这边文章介绍创建一个基于RESTful的网络服务web service,并介绍一些相关的概念。

最终我们需要实现:访问"http://localhost:8080/greeting?name=Hsiao"后,在浏览器显示以下内容:

rest-service_RESTful_api_greeting_with_name

参考:https://spring.io/guides/gs/rest-service

什么是RESTful?

什么是web service?

一、Spring初始化

官方的的页面:https://start.spring.io/ 我们按照以下配置

  • Project:选择Marven
  • Language:选择Java
  • Spring Boot:选择3.2.3
  • Project Metadata:Artifact设为rest-servicePackaging选择JarJava选择17
  • Dependencies:添加Spring Web
rest-service_RESTful_demo_initialize

点击Generate后会下载rest-service.zip,将其解压得到rest-service并在VS Code中打开后,默认是这样的:

二、创建record Greeting

我们这个web service网络服务将要响应/greeting接口GET请求,该请求有一个可选的String类型参数name

在我们的响应中,响应头200 OK表示响应成功,同时我们需要把一个Greeting实例greetingObj放在响应体

Greeting实例的一个实例如下:

{
    "id": 1,
    "content": "Hello, World!",
}

其中的id字段是生成的问候语的唯一标识符,它是全局自增不会重复的,换言之每次响应这个接口返回的id都会不一样,且是全局自增的。content是问候的具体内容

创建record Greeting

java/com/example/restservice处右键,依次选择New Java File -> Record

rest-service_RESTful_add_Java_record

给record命名Greeting

rest-service_RESTful_add_Java_record_Greeting

默认的Greeting:

rest-service_RESTful_record_Greeting_default

我们将其修改成Greeting.java

package com.example.restservice;

public record Greeting(long id, String content) {

}
rest-service_RESTful_record_Greeting_edit

三、创建class GreetingController

java/com/example/restservice处右键,依次选择New Java File -> Class

rest-service_RESTful_add_Java_class

给class命名GreetingController

rest-service_RESTful_add_Java_class_GreetingController

将其修改成 GreetingController.java

package com.example.restservice;

import java.util.concurrent.atomic.AtomicLong;

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

@RestController
public class GreetingController {

    private static final String template = "Hello, %s";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

}
rest-service_RESTful_class_GreetingController_edit

四、运行

VS Code点击运行后,访问"http://localhost:8080/greeting"将会显示以下:

rest-service_RESTful_api_greeting

若在访问该接口时带上参数name,如"http://localhost:8080/greeting?name=Hsioao",我们的greeting()方法能解析到name参数是“Hsiao”,所以不再使用默认的参数“World”:

rest-service_RESTful_api_greeting_with_name

五、总结

Spring Boot quick start

前言

Spring是一个Java框架,可以用于MicroservicesReactiveCloudWeb AppServerlessEventDrivenBatch等,具有应用广泛、高效、灵活、高速、安全、丰富的社区支持等特性

官网:https://spring.io

当前文章的主要内容是启动一个Spring Boot项目,它具有一个/hello接口,这个接口支持传递name参数,如果没有读取到name参数,则使用默认值的World,并基于name参数返回一个字符串

1、下载demo

在官网http://start.spring.io生成一个demo。

该页面的Project选择MavenSpring Boot选择3.2.3,在Add Dependence中搜索web并选择Spring Web,最后点击下方的Generate按钮就会下在一个demo.zip

spring boot generate

解压后得到的文件结构如下:

demo

2、新增hello函数及接口

这里使用VS Code打开demo,并修改源码如下:

DemoApplication.java

DemoApplication.java

package com.example.demo;

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 DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return String.format("Hello %s", name);
	}
}

介绍一下新增的hello()方法

该方法接收一个String类型的参数name,并用字符串Hello与其拼接,返回拼接之后的字符串。举个例子如果传入的name=Jack,那么最终返回的是一个"Hello Jack"字符串

类名之上@RestController声明意味着当前代码是一个支持web后端代码

方法声明之前@GetMapping("/hello")表示我们的hello()方法是用于响应发送到"http://localhost:8080/hello"地址的请求。

hell0()函数签名中,位于name之前的@RequestParam(value = "name", defaultValue = "World")意味着希望从请求中读取name字段,若未读取到该字段则使用默认的字符串"World"传递给当前函数的参数name

在我们编辑代码的同时会自动导入所需要的包,如顶部的import org.springframework.web.bind.annotation.RestController;等。

3、启动并调试该接口

点击VS Code右上角的三角形▶️按钮即可运行当前程序。运行成功后会在控制台的terminal显示以下内容:

spring boot run

如果需要停止运行,点击以下Disconnect按钮即可:

spring disconnect

在程序运行过程中,我们在浏览器访问"http://localhost:8080/hello",将会看到以下内容:

http://localhost:8080/hello

这表示我们访问本地服务器的/hello接口,并且没有带任何参数。由于我们的hello()方法在没有读取到name参数时会使用默认的"World"字符串当作默认参数,所以最终我们看到浏览器显示了拼接Hello之后的字符串"Hello World"

我们再尝试带上指定的name参数Hsiao:再次访问"http://localhost:8080/hello?name=Hsiao",此时浏览器将显示:

“http://localhost:8080/hello?name=Hsiao”