How to retrieve raw post data from HttpServletRequest in java

前端 未结 3 516
栀梦
栀梦 2020-12-04 13:21

I\'m trying to get the post data in Java. Seems like it should be one of the simplest things to do right? I mean, HttpServletRequest.getParameter has to do it right? So how

3条回答
  •  既然无缘
    2020-12-04 13:37

    This worked for me: (notice that java 8 is required)

    String requestData = request.getReader().lines().collect(Collectors.joining());
    UserJsonParser u = gson.fromJson(requestData, UserJsonParser.class);
    

    UserJsonParse is a class that shows gson how to parse the json formant.

    class is like that:

    public class UserJsonParser {
    
        private String username;
        private String name;
        private String lastname;
        private String mail;
        private String pass1;
    //then put setters and getters
    }
    

    the json string that is parsed is like that:

    $jsonData: {    "username": "testuser",    "pass1": "clave1234" }
    

    The rest of values (mail, lastname, name) are set to null

提交回复
热议问题