无法从servlet读取请求体

时间:2015-12-10 09:45:47

标签: java spring http servlets

这是我的控制器代码:

@RequestMapping(value = "/test", method = RequestMethod.POST)
    public @ResponseBody String testPost(@RequestParam(value = "testParam") int testParam, HttpServletRequest request) {    
        try {
            System.out.println("body is "+BufferUtil.getHttpRequestBody(request));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

BufferUtil.getHttpRequestBody(request)

public static String getHttpRequestBody(HttpServletRequest request)
            throws IOException {
        Scanner s = null;
        try {
            s = new Scanner(request.getInputStream(), "UTF-8")
                    .useDelimiter("\\A");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s.hasNext() ? s.next() : "";
    }

这是用于测试控制器的代码:

 HTTPUtil.sendRequest("http://localhost:8081/es09android/test?testParam=1", "POST", "hello world");

sendRequest()实施:

public static HttpResponse sendRequest(String url, String method,
                                           String data) {
        DataOutputStream wr = null;
        BufferedReader in = null;
        HttpResponse httpResponse = new HttpResponse();
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod(method);
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            con.setDoOutput(true);
            if (data != null) {
                wr = new DataOutputStream(con.getOutputStream());
                wr.write(data.getBytes(UTF8_CHARSET));
            }
            int responseCode = con.getResponseCode();
            httpResponse.setResponseCode(responseCode);
            if (httpResponse.isOk()) {
                in = new BufferedReader(new InputStreamReader(
                        con.getInputStream(), "UTF8"));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                httpResponse.setData(response.toString());
            }
            return httpResponse;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

如果我运行它然后发送请求,控制器将不会打印正文。但是如果从控制器中删除@RequestParam(value = "testParam"),一切都会好的。有什么问题?

1 个答案:

答案 0 :(得分:0)

在以下评论中提供的更多信息后编辑

我认为正在发生的是s.hasNext()正在返回false,因为你在请求的InputStream的末尾。这是按照设计的,你只能读一次InputStream。

This question有一个很好的例子,可以让您多次缓存请求以便多次读取它 This question描述了一种使用Spring记录整个请求的方法,这似乎就是您的问题所在。

相关问题