Java servlet 读取 传入Json数据

Java servlet 读取 传入Json数据

    private String readBody(HttpServletRequest req) throws IOException {
        if(req.getMethod().equals("POST")){
            StringBuilder result = new StringBuilder();
            BufferedReader br = req.getReader();
            String line;
            while((line = br.readLine()) != null){
                result.append(line);
            }
            br.close();
            //System.out.println(result.toString());
            return result.toString();
        }
        return null;
    }

调用:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String body=readBody(req);
}

你可能感兴趣的:(#,Java,Web,servlet,java,json)