调用 Spring 服务(导入 java.net.URL.* 包) POST 其调用但在响应时收到 java.io.FileNotFoundException
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试从上面示例中的输入流中读取数据之前,请关闭输出流。
作为替代方案,使用 http 客户端库,例如 HTTPClient 或 Resty。
使用 Resty,您的客户端代码将如下所示:
对于 GET
对于使用简单形式的帖子:
免责声明:我是 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:
for a GET
and for a POST using a simple form:
Disclaimer: I'm the author of Resty