标签归档:springboot

SpringBoot 取出 JSONString 形式的 requestBody

@PostMapping("/getRequestBodyJson")
public void getRequestBodyJson(HttpServletRequest request) throws IOException {
    BufferedReader reader = new BufferedReader(
        new InputStreamReader(request.getInputStream(), "UTF-8"));
    String line;
    String result = "";
    while ((line = reader.readLine()) != null) {
        result += line;
    }
    System.out.println(result);
}

注意不能使用 @RequestBody 注解,否则会空指针错误。因为 request.getInputStream() 只能被读取一次,然后就会被清空。