出自:https://blog.csdn.net/qq_45309010/article/details/111301381
https://segmentfault.com/q/1010000023764476
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.litong.utils.io.IOUtils;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
// 这里我没做异常处理
@RequestMapping("/request")
public String request(HttpServletRequest request, HttpServletResponse response) {
String method = request.getMethod();
log.info("method : " + method);
StringBuilder requestBody = new StringBuilder();
BufferedReader buffredReader = null;
try {
buffredReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
String inputStr;
while ((inputStr = buffredReader.readLine()) != null) {
requestBody.append(inputStr);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(buffredReader);
}
return requestBody.toString();
}
}
RFC 文档中从来就没有说过 GET 没有请求体.RFC 只是说 只是说 GET 意味着通过 URI 来识别资源。 这就意味着,各种代理缓存以及 CDN 都是通过 URI 区分出资源.如果你把参数丢在了 REQUEST BODY里,那这些服务都要混乱了..
有些 http 的 lib 里不让甚至直接不提供 GET 方法追加请求体的操作,单实际上,这并不是错误.