获取请求负载从的Java servlet POST请求(Getting request payloa

2019-07-18 17:55发布

我有一个发送POST请求我的Java servlet一个JavaScript库,但在doPost方法,我似乎无法得到请求负载的内容。 在Chrome开发者工具,所有的内容都在标题标签请求负载部分,内容在那里,我知道了POST正在doPost方法获得,但它只是出现空白。

对于HttpServletRequest对象,有什么办法可以让我在请求负载中的数据?

这样做request.getParameter()request.getAttributes()都没有数据结束

Answer 1:

使用getReader()读取请求的主体



Answer 2:

String payloadRequest = getBody(request);

使用这种方法

public static String getBody(HttpServletRequest request) throws IOException {

    String body = null;
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }

    body = stringBuilder.toString();
    return body;
}


Answer 3:

您可以使用缓冲区读取器从请求读取

    // Read from request
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader = request.getReader();
    String line;
    while ((line = reader.readLine()) != null) {
        buffer.append(line);
    }
    String data = buffer.toString()


Answer 4:

Java的8流

String body = request.getReader().lines()
    .reduce("", (accumulator, actual) -> accumulator + actual);


Answer 5:

随着Apache的百科全书IO您可以在一个行做到这一点。

IOUtils.toString(request.getReader())


Answer 6:

如果身体中的内容是Java 8字符串,你可以这样做:

String body = request.getReader().lines().collect(Collectors.joining());



Answer 7:

如果你能够发送有效载荷的JSON,这是一个阅读playload最方便的方法:

示例数据类:

public class Person {
    String firstName;
    String lastName;
    // Getters and setters ...
}

例如有效载荷(请求体):

{ "firstName" : "John", "lastName" : "Doe" }

代码阅读servlet的有效载荷(需要com.google.gson。*):

Person person = new Gson().fromJson(request.getReader(), Person.class);

就这样。 漂亮,简单,干净。 不要忘记设置Content-Type头应用程序/ JSON。



Answer 8:

使用Java 8尝试与资源:

    StringBuilder stringBuilder = new StringBuilder();
    try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
        char[] charBuffer = new char[1024];
        int bytesRead;
        while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
            stringBuilder.append(charBuffer, 0, bytesRead);
        }
    }


Answer 9:

你只需要

request.getParameterMap()

用于获取POST和GET - 参数。

该方法返回一个Map<String,String[]>

您可以通过阅读在地图中的参数

Map<String, String[]> map = request.getParameterMap();
//Reading the Map
//Works for GET && POST Method
for(String paramName:map.keySet()) {
    String[] paramValues = map.get(paramName);

    //Get Values of Param Name
    for(String valueOfParam:paramValues) {
        //Output the Values
        System.out.println("Value of Param with Name "+paramName+": "+valueOfParam);
    }
}


文章来源: Getting request payload from POST request in Java servlet