【发布时间】:2016-10-08 18:34:21
【问题描述】:
我正在尝试向服务器发布 http 帖子,但我从控制器收到格式错误的 url 异常
控制器代码
public static final String REST_SERVICE_URI = "localhost:8081/create";
控制器中从服务器接收请求的方法
@RequestMapping(value="AddService",method = RequestMethod.POST)
@ResponseBody
public void addService(@ModelAttribute("servDetForm")) throws IOException{
//return dataServices.addService(tb);
URL serv;
URLConnection yc;
try {
serv = new URL(REST_SERVICE_URI);
yc = serv.openConnection();
try {
yc = serv.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in;
in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我的jsp视图
<form:form method="POST" commandName="servDetForm" action="AddService">
<table style="appearance:dialog ">
<tr>
<td>Number</td>
<td><form:input path="Numbers"/></td>
</tr>
我哪里错了?
【问题讨论】:
-
localhost 是主机名而不是协议。您需要在 REST_SERVICE_URI 中指定协议 http/https 等。所以应该是
http://localhost:8081/ItaxServ/create -
使用这个 localhost:8081/ItaxServ/create 会出现类似 java.io.IOException: Server returned HTTP response code: 400 for URL: localhost:8081/ItaxServ/create 的错误
标签: java spring jsp http spring-mvc