我有这个 Spring Controller :
@RequestMapping(value = "/create")
@ResponseBody
public ResponseEntity<?> create(
@RequestParam String name,
@RequestParam Integer startYear,
@RequestParam Integer endYear,
@RequestParam(required=false) MultipartFile polygons,
@RequestParam(required=false) Long reference
)
是否可以通过 POST 请求从 Angular 发送这些参数?
我正在尝试:
public createExperiment(): Observable<any> {
const headers = new HttpHeaders({'Content-Type': 'application/json'});
return this.http.post(this.backUrl + 'puerto/create', {name:'name'}, {headers: headers})
.map((res: any) =>
res
);
}
但是我已经收到这个错误:
error : {timestamp: 1527665099011, status: 400, error: "Bad Request", exception: "org.springframework.web.bind.MissingServletRequestParameterException", message: "Required String parameter 'name' is not present", …}
为什么会出现这个错误?如果我解决了这个问题,我还能发送 MultiPartFile 吗?
我知道我可以通过更改 Controller 和通过 ResquestBody 接收参数来解决这个问题,但我想单独发送参数。
最佳答案
@RequestParam
当你有 url 参数时使用。
http.post(url, null, { headers: headers, params: new HttpParams().set('name', 'fer') })
这将发送名称和 url,如 http://abcd.com/a/b?name=fer
如果您想将其作为帖子正文发送,请在您的 spring 代码中定义一个模型并在 spring Controller 中使用 @RequestBody
注释。
看here举个例子。
关于spring - 如何通过 POST 从 Angular 5 向 Spring Controller 发送多个 RequestParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50599015/