簡體   English   中英

無法從servlet讀取請求正文

[英]Can not read request body from servlet

這是我的控制器代碼:

@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") ,一切都會好起來的。 怎么了?

在以下評論中提供了更多信息之后進行了編輯

我假設發生的情況是s.hasNext()返回false ,因為您位於請求的InputStream的末尾。 按照設計,您只能讀取InputStream一次。

這個問題有一個很好的例子,說明了您可以緩存請求以多次讀取的方法。
這個問題描述了一種使用Spring記錄整個請求的方法,這似乎是您的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM