我正在尝试将一个简单的String从HTML表单发送到基于spring的REST Web服务,但即使经过多次尝试,我也无法将数据发送到我的webservice,每次我得到"请求客户发送的语法不正确" (400)错误。有人可以查看代码片段并让我知道我错过了什么吗?
<form action="http://localhost:8080/SpringRest/rest/store/prolist" method="GET">
<p>
Products : <input type="text" name="listofproducts" id = "listofproducts"/>
</p>
<input type="submit" value="Get the Lowest Price" />
</form>
控制器中的代码:
@RequestMapping(value = "http://localhost:8080/SpringRest/rest/store/prolist/{listofproducts}", method = RequestMethod.GET)
public @ResponseBody String getListOfProducts(@PathVariable String listofproducts) {
return listofproducts;
}
答案 0 :(得分:2)
使用GET发送表单数据时,您必须将其作为请求参数而不是路径变量接收,因为表单数据将作为请求参数以下列格式发送到服务器
http://host_port_and_url?name1=value1&name2=value2&more_params
所以这样做
@RequestMapping(value = "http://localhost:8080/SpringRest/rest/store/prolist", method = RequestMethod.GET)
public @ResponseBody String getListOfProducts(@RequestParam(value = "listofproducts") String listofproducts) {
return listofproducts;
}
您可以查看this
关于GET的说明:
Appends form-data into the URL in name/value pairs The length of a URL is limited (about 3000 characters) Never use GET to send sensitive data! (will be visible in the URL) Useful for form submissions where a user want to bookmark the result GET is better for non-secure data, like query strings in Google
此外,您不需要与主机,端口等进行绝对URI映射。只需使用控制器映射即可。假设store
是控制器上下文路径,这就足够了
@RequestMapping(value = "store/prolist", method = RequestMethod.GET)