调用 Spring 服务(导入 java.net.URL.* 包) POST 其调用但在响应时收到 java.io.FileNotFoundException

发布于 2024-11-07 06:34:33 字数 1738 浏览 0 评论 0原文

我正在尝试使用 URL 包设置对 Spring 服务的基本调用,以便我可以通过 POST 而不是 get 来完成此操作。

客户端代码(调用 spring 服务的代码):

        String data = URLEncoder.encode("testStringFromGWT", "UTF-8") + "=" + URLEncoder.encode(message, "UTF-8");

        URL url = new URL("http://localhost:8080/spring-hibernate-mysql/test/test1");

        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();

Spring 服务:

@RequestMapping(value = "/test1", method = RequestMethod.POST)
public String loggedInUniversal_logout(
        Model model,
        HttpServletRequest request,
        @RequestParam(value = "inputString", required = true) String inputString)
        throws InterruptedException {

    HttpSession session = request.getSession();

    System.out.println("Request made from Client..." + inputString);

    model.addAttribute("token", "It works");
    return "token";
}

当我尝试这个时,我得到:

java.io.FileNotFoundException: http://localhost:8080/spring-hibernate-mysql/test/test1
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)

我不太确定我做错了什么,我能够确认调用是否正确传递给 Spring我可以看到正在打印的行 "Request made from Client..." + inputString 但随后我在客户端上收到 FileNotFoundException。我通过查看教程将其拼凑在一起,所以我想我在这里遗漏了一些东西,将不胜感激任何建议。

I'm trying to setup a basic call to a Spring service using the URL package so that I can do it through a POST rather than get.

Client code (the code calling the spring service):

        String data = URLEncoder.encode("testStringFromGWT", "UTF-8") + "=" + URLEncoder.encode(message, "UTF-8");

        URL url = new URL("http://localhost:8080/spring-hibernate-mysql/test/test1");

        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();

The Spring service:

@RequestMapping(value = "/test1", method = RequestMethod.POST)
public String loggedInUniversal_logout(
        Model model,
        HttpServletRequest request,
        @RequestParam(value = "inputString", required = true) String inputString)
        throws InterruptedException {

    HttpSession session = request.getSession();

    System.out.println("Request made from Client..." + inputString);

    model.addAttribute("token", "It works");
    return "token";
}

When I try this I get:

java.io.FileNotFoundException: http://localhost:8080/spring-hibernate-mysql/test/test1
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)

I'm not quite sure what I am doing wrong, I am able to confirm that the call is being passed through properly to Spring as I can see the line being printed "Request made from Client..." + inputString but then I get the FileNotFoundException on the client. I pieced this together from looking at tutorials so I guess I am missing something here, would appreciate any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-11-14 06:34:33

在尝试从上面示例中的输入流中读取数据之前,请关闭输出流。

作为替代方案,使用 http 客户端库,例如 HTTPClientResty

使用 Resty,您的客户端代码将如下所示:

Resty r = new Resty();
String result = r.text(url).toString();

对于 GET
对于使用简单形式的帖子:

r.text(url,form(yourformdata)).toString();

免责声明:我是 Resty 的作者

Close the output stream before trying to read from the input stream in your example above.

As an alternative, use a http client library like HTTPClient or Resty.

With Resty, your client code would look like this:

Resty r = new Resty();
String result = r.text(url).toString();

for a GET
and for a POST using a simple form:

r.text(url,form(yourformdata)).toString();

Disclaimer: I'm the author of Resty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文