我想在spring boot中通过POST方法发送params,但param一直都是null。
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam(value = "path", defaultValue = "") String path) {
return "hello " + path;
}
当我尝试测试时出现了这个错误
{
"timestamp": 1512262419197,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.bind.MissingPathVariableException",
"message": "Missing URI template variable 'path' for method parameter of type String",
"path": "/upload"
}
答案 0 :(得分:0)
获得此"路径"的唯一途径变量是按以下方式构建一个URL
..../upload?path=foo
如果你想收到一个身体,那么你将不得不使用@RequestBody
如果您想使用../{id}/upload
之类的路径变量,请使用@PathVariable
答案 1 :(得分:0)
我认为你需要在参数中使用Data Class。因为 POST方法不能像那样使用。 url的参数调用仅适用于 GET方法。你需要从jquery示例中调用它。或者为了方便起见,您可以使用Postman等工具
@RestController
@RequestMapping(value ="/api")
public class ApiDataController
{
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@ModelAttribute UploadForm form)
{
return "hello " + form.path;
}
public class UploadForm
{
public string path = "";
public UploadForm() {
// TODO Auto-generated constructor stub
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
}