springboot~通过集成测试来理解Accept和Content-Type
<h2 id="-核心区别">🎯 核心区别</h2><ul>
<li><strong><code>Content-Type</code></strong>:告诉服务器<strong>我发送的数据是什么格式</strong></li>
<li><strong><code>Accept</code></strong>:告诉服务器<strong>我希望接收什么格式的响应数据</strong></li>
</ul>
<h2 id="-详细说明">📋 详细说明</h2>
<h3 id="1-content-type-内容类型">1. <code>Content-Type</code> (内容类型)</h3>
<ul>
<li><strong>作用</strong>:描述<strong>请求体</strong>的格式</li>
<li><strong>使用场景</strong>:当你的请求有请求体时(如POST、PUT请求)</li>
<li><strong>示例</strong>:<code>Content-Type: application/json</code> 表示"我发送的是JSON格式的数据"</li>
</ul>
<pre><code class="language-java">// 在MockMvc中设置Content-Type
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)// 告诉服务器请求体是JSON
.content("{\"name\":\"John\", \"age\":30}"))
</code></pre>
<h3 id="2-accept-接受类型">2. <code>Accept</code> (接受类型)</h3>
<ul>
<li><strong>作用</strong>:描述<strong>客户端期望的响应</strong>格式</li>
<li><strong>使用场景</strong>:任何请求(GET、POST、PUT、DELETE等)</li>
<li><strong>示例</strong>:<code>Accept: application/json</code> 表示"我希望接收JSON格式的响应"</li>
</ul>
<pre><code class="language-java">// 在MockMvc中设置Accept
mockMvc.perform(get("/api/users/1")
.accept(MediaType.APPLICATION_JSON))// 期望服务器返回JSON
</code></pre>
<h2 id="-实际应用场景">🔄 实际应用场景</h2>
<h3 id="场景1post请求发送json期望返回json">场景1:POST请求发送JSON,期望返回JSON</h3>
<pre><code class="language-java">// 这种情况需要同时设置Content-Type和Accept
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)// 我发送JSON
.accept(MediaType.APPLICATION_JSON) // 我希望收到JSON
.content("{\"name\":\"John\", \"age\":30}"))
.andExpect(status().isCreated());
</code></pre>
<h3 id="场景2get请求期望返回json">场景2:GET请求,期望返回JSON</h3>
<pre><code class="language-java">// 只有请求,没有请求体,所以只需要Accept
mockMvc.perform(get("/api/users")
.accept(MediaType.APPLICATION_JSON)) // 只设置Accept
.andExpect(status().isOk());
</code></pre>
<h3 id="场景3post请求发送json不关心响应格式">场景3:POST请求发送JSON,不关心响应格式</h3>
<pre><code class="language-java">// 只设置Content-Type,不设置Accept
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)// 只设置Content-Type
.content("{\"name\":\"John\", \"age\":30}"));
</code></pre>
<h2 id="-总结表格">📊 总结表格</h2>
<table>
<thead>
<tr>
<th>参数</th>
<th>作用</th>
<th>使用场景</th>
<th>示例值</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Content-Type</strong></td>
<td>描述<strong>请求体</strong>格式</td>
<td>POST、PUT等有请求体的操作</td>
<td><code>application/json</code></td>
</tr>
<tr>
<td><strong>Accept</strong></td>
<td>描述期望的<strong>响应</strong>格式</td>
<td>任何需要特定响应格式的操作</td>
<td><code>application/json</code></td>
</tr>
</tbody>
</table>
<h2 id="️-实际代码示例">🛠️ 实际代码示例</h2>
<h3 id="完整的post请求测试示例">完整的POST请求测试示例</h3>
<pre><code class="language-java">@Test
public void testCreateUser() throws Exception {
// 准备请求数据
UserCreateRequest request = new UserCreateRequest("John", "john@example.com");
String requestJson = new ObjectMapper().writeValueAsString(request);
// 执行请求
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)// 必须:请求体是JSON
.accept(MediaType.APPLICATION_JSON) // 可选:期望JSON响应
.content(requestJson))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists())
.andExpect(jsonPath("$.name").value("John"));
}
</code></pre>
<h2 id="-记忆技巧">💡 记忆技巧</h2>
<ul>
<li><strong>Content-Type</strong> → 我<strong>发送</strong>什么 → 关注<strong>请求体</strong></li>
<li><strong>Accept</strong> → 我<strong>接受</strong>什么 → 关注<strong>响应体</strong></li>
</ul>
<h2 id="️-注意事项">⚠️ 注意事项</h2>
<ol>
<li><strong>POST请求必须设置Content-Type</strong>,否则服务器不知道如何解析请求体</li>
<li><strong>Accept是可选的</strong>,如果不设置,服务器通常会返回默认格式</li>
<li>如果服务器不支持客户端请求的Accept格式,应该返回406状态码</li>
</ol>
<p>所以,对于你的POST请求构建JSON的情况,<strong>必须设置<code>Content-Type: application/json</code></strong>,而<code>Accept</code>根据你是否对响应格式有要求来决定是否设置。</p>
</div>
<div id="MySignature" role="contentinfo">
<p></p>
<div class="navgood">
<p>作者:仓储大叔,张占岭,<br>
荣誉:微软MVP<br>QQ:853066980</p>
<p><strong>支付宝扫一扫,为大叔打赏!</strong>
<br><img src="https://images.cnblogs.com/cnblogs_com/lori/237884/o_IMG_7144.JPG"></p>
</div><br><br>
来源:https://www.cnblogs.com/lori/p/19237278
頁:
[1]