Spring Boot Rest API 示例——你好世界

在本节中,我们将学习通过以下 5 个简单步骤创建一个基于 Spring Boot hello world rest api 的应用程序。

开始了 !

第 1 步:打开 Spring Initializr

打开 Spring Initializr 并创建一个 Spring Boot Rest 项目,如下图所示。添加 REST 依赖项并生成项目。

添加 maven 依赖项,这是在生成上述项目时添加的两个依赖项。

<?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 https://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.6.4</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example.rest</groupId>
	<artifactId>springbootrest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springbootrest</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</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>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Spring Boot REST Controller——制作一个 REST 控制器

  • @RestController 注解用于控制器类。
  • @GetMapping & @PostMapping 用于声明 get 和 post api。
  • 属性 消费 和 生产 用于消费和生产不同类型的数据,例如 json、xml、html。

第 2 步:创建获取 API

如下创建一个类,将代码复制并粘贴到您的 IDE 中。

package com.example.rest.springbootrest;

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

@RestController
@RequestMapping("api")
public class HelloController {

	@GetMapping("/hello")
	public String helloWorld() {

		return "你好,世界!";

	}

}

第 3 步:使用 @SpringBootApplication 的主要方法

使用下面的 main 方法创建一个类,将代码复制并粘贴到您的 IDE 中,
这里的关键是我们使用 @SpringBootApplication 注解通知框架这是我们的主要 springboot 类

package com.example.rest.springbootrest;

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

@SpringBootApplication
public class SpringbootrestApplication {

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

}

结果

您可以下载 REST 客户端或创建自己的客户端并测试 GET 和 POST api,如下所示。

GET API 响应 – 当我们点击 GET api 时,请查看下面的快照

 

概括

在本节中,我们学习了如何使用 SPRING BOOT 创建一个简单的基于 REST 的项目。希望你喜欢它!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值