【发布时间】:2018-06-29 02:40:14
【问题描述】:
我想从 Spring Boot 控制器读取 POST 数据。
我已经尝试了这里给出的所有解决方案:HttpServletRequest get JSON POST data,但我仍然无法读取 Spring Boot servlet 中的发布数据。
我的代码在这里:
package com.testmockmvc.testrequest.controller;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Controller
public class TestRequestController {
@RequestMapping(path = "/testrequest")
@ResponseBody
public String testGetRequest(HttpServletRequest request) throws IOException {
final byte[] requestContent;
requestContent = IOUtils.toByteArray(request.getReader());
return new String(requestContent, StandardCharsets.UTF_8);
}
}
我尝试过使用收集器作为替代方案,但这也不起作用。我做错了什么?
【问题讨论】:
-
我总是得到零长度的内容。如果我调试代码,我可以在 servlet 请求对象中看到发布数据,但不知何故它没有被读取。
-
get 请求没有正文,是吗?
-
澄清:这是一个帖子(不指定方法允许GET和POST)
-
我只是说你应该用get调用它!
标签: spring spring-boot spring-restcontroller