springboot后端的接口headers

问到了前后端通信中一个常被初学者混淆 的重点------

为什么 fetch('/api/show', { headers: { 'Accept': 'text/plain' } }),而不是
headers: { 'Content-Type': 'application/json' }


🧩 一句话结论

👉 因为你的接口:

复制代码
@GetMapping("/show")
public String action() {
    return "Hello Spring Boot!";
}

是一个 GET 请求,且返回纯文本(text/plain)

所以前端只需要告诉服务器「我能接受纯文本响应」(Accept),

而不需要告诉服务器「我发的是 JSON 数据」(Content-Type)。


📘 详细解释

Accept:表示客户端期望接收的数据格式

在你的 fetch 请求中:

复制代码
headers: {
  'Accept': 'text/plain'
}

意思是:

"我(浏览器)希望服务器返回纯文本(text/plain)。"

而你的 Spring Boot 接口:

复制代码
@GetMapping("/show")
public String action() {
    return "Hello Spring Boot!";
}

默认就是返回一个 text/plain 类型的响应。

👉 所以这两个是匹配的


Content-Type:表示客户端发送的数据格式

Content-Type 只在你**有请求体(body)**时才有意义。

例如:

复制代码
fetch('/api/register', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ username: 'Tom', password: '123456' })
})

这时你告诉后端:

"我发给你的内容是 JSON 格式,请按 JSON 来解析。"

Spring Boot 中的 @RequestBody User user 就是用来解析这种 JSON 请求的。


✅ 结论总结

请求类型 是否需要请求体 用什么 header 说明
GET /api/show ❌ 无请求体 'Accept': 'text/plain' 告诉后端希望拿到纯文本
POST /api/register ✅ 有请求体(JSON) 'Content-Type': 'application/json' 告诉后端发送的是 JSON

🔧 实际建议

其实即便你不写:

复制代码
headers: { 'Accept': 'text/plain' }

浏览器默认也会发送 Accept: */*,Spring Boot 仍然能正确返回 "Hello Spring Boot!"

关于接口的get到html文件代码如下:

复制代码
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>Show API Demo</title>
    <style>
        body {
            font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            display: flex;
            min-height: 100vh;
            align-items: center;
            justify-content: center;
            background: #f6f8fa;
            margin: 0;
        }
        .card {
            background: #fff;
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 6px 24px rgba(0,0,0,0.08);
            width: 360px;
            text-align: center;
        }
        h1 { margin: 0 0 12px; font-size: 20px; }
        #result {
            margin: 16px 0;
            padding: 12px;
            border-radius: 8px;
            background: #f0f4ff;
            min-height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #1a1a1a;
        }
        button {
            padding: 8px 14px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            background: #2b82ff;
            color: #fff;
            font-weight: 600;
        }
        .hint { margin-top: 10px; color: #666; font-size: 13px; }
    </style>
</head>
<body>
<div class="card">
    <h1>调用后端接口 /api/show</h1>

    <div id="result">正在加载...</div>

    <div>
        <button id="refreshBtn">刷新</button>
    </div>

    <div class="hint">注意:如果后端与此页面不在同一域或端口,请开启 CORS。</div>
</div>

<script>
    const resultEl = document.getElementById('result');
    const btn = document.getElementById('refreshBtn');

    async function fetchShow() {
        resultEl.textContent = '加载中...';
        try {
            // 如果你的控制器映射在 /api 下,请使用 /api/show
            const resp = await fetch('/api/show', {
                method: 'GET',
                headers: {
                    'Accept': 'text/plain'
                }
            });

            if (!resp.ok) {
                resultEl.textContent = `请求失败:${resp.status} ${resp.statusText}`;
                return;
            }

            // 接口返回的是字符串,所以用 text()
            const text = await resp.text();
            resultEl.textContent = text;
        } catch (err) {
            console.error(err);
            resultEl.textContent = '网络错误,无法连接到后端。检查服务器是否运行或 CORS 是否已开启。';
        }
    }

    // 页面加载时自动请求一次
    window.addEventListener('load', fetchShow);
    btn.addEventListener('click', fetchShow);
</script>
</body>
</html>

效果图如下:

相关推荐
daidaidaiyu10 分钟前
Jetlinks 物联网平台 开源版学习源码分析
java·mqtt·rxjava·tcp
j***788842 分钟前
clickhouse-介绍、安装、数据类型、sql
java
2***B4491 小时前
Rust在系统编程中的内存安全
开发语言·后端·rust
U***e631 小时前
Rust错误处理最佳实践
开发语言·后端·rust
q***47181 小时前
Spring中的IOC详解
java·后端·spring
vx_vxbs662 小时前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea
SunnyDays10112 小时前
如何使用 Java 删除 Word 文档中的水印
java·删除word文档水印
码事漫谈2 小时前
C++小白最容易踩的10个坑(附避坑指南)
后端
码事漫谈2 小时前
性能提升11.4%!C++ Vector的reserve()方法让我大吃一惊
后端
毕设源码-邱学长3 小时前
【开题答辩全过程】以 基于Java企业人事工资管理系统为例,包含答辩的问题和答案
java·开发语言