【spring-boot】简单get请求接口

本文档将指导您如何使用Spring Boot创建一个支持GET请求的接口。首先,定义响应体对象,接着创建@RestController注解的Controller来封装响应。然后,配置POM.xml,运行服务并使用http://localhost:8080/greeting?name=Users进行测试。讲解了@RestController与@Controller的区别,以及@GetMapping和@RequestParam注解的用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:用spring-boot搭建一个支持get接口请求的服务。

1.构造一个响应体对象:

package com.example.restservice;

public class Greeting {

	private final long id;
	private final String content;

	public Greeting(long id, String content) {
		this.id = id;
		this.content = content;
	}

	public long getId() {
		return id;
	}

	public String getContent() {
		return content;
	}
}

2.接口请求响应封装controller:

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));
    }
}

3.服务应用启动程序:

package com.example.restservice;

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

@SpringBootApplication
public class RestServiceApplication {

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

}

4.POM.xml配置

<?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.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>rest-service-complete</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>rest-service-complete</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>

5.服务部署并启动:

mvnw spring-boot:run

6.通过get请求访问服务:

http://localhost:8080/greeting?name=Users

Up:

@RestController:

相当于@Controller+@ResponseBody两个注解的结合,但不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@GetMapping:

用于处理请求方法的GET类型

@RequestParam:

获取springmvc后台数据

@SpringBootApplication:

开启自动配置

Outer:

@Controller和@RestController的区别?

参考:@Controller和@RestController的区别?_hery186的专栏-优快云博客_restcontroller

@GetMapping注解的理解

参考:@GetMapping注解的理解_水巷石子的博客-优快云博客_getmapping

@RequestParam注解的使用:

参考:@RequestParam 注解的使用_Lovincc的博客-优快云博客

@SpringBootApplication启动类执行流程:

参考:SpringBoot 启动类 @SpringBootApplication 注解 以及执行流程_殇莫忆的博客-优快云博客_springbootapplication

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值