我正在使用Java Maven Spring启动SpringMVC中的REST api。 SpringMVC @RequestParam不会看到Angular POST请求参数。这是Angular代码;
saveAsSiteProduct(id: number, data: saveAsSiteProductSettings): Observable<any> {
const options = new RequestOptions({ headers: new Headers({ 'Content-Type': 'application/json' }) });
return this.http
.post(this.amazonUrl + '/products/imported/' + id +'/saveas/siteproduct', JSON.stringify(data), options)
.map(response => response.json());
}
这是Java SpringMVC代码;
@PostMapping(value = "/products/imported/{id}/saveas/siteproduct", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<CustomHttpResponse> saveAsSiteProduct(
@PathVariable Long id,
@RequestParam Map<String, String> requestParams,
@RequestParam("guid") String guid
) throws EncoderException, RestClientException {
...
}
但是我得到了回应;
{
"timestamp": "2017-10-02T21:21:58.064+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MissingServletRequestParameterException",
"message": "Required String parameter 'guid' is not present",
"path": "/api/amazon/products/imported/1/saveas/siteproduct"
}
这就是我发布的内容;
{
createBrandIfNotExists : true
createManufacturerIfNotExists : true
guid : "asD"
}
我在Postman中测试了这个并且它有效。
答案 0 :(得分:0)
我改为使用URLSearchParams
并将内容类型更改为application/x-www-form-urlencoded
。我是这样做的;
saveAsSiteProduct(id: number, data: saveAsSiteProductSettings): Observable<any> {
const options = new RequestOptions({ headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }) });
const params = new URLSearchParams();
for (var key in data) {
params.set(key, data[key]);
}
return this.http
.post(this.amazonUrl + '/products/imported/' + id +'/saveas/siteproduct', params, options)
.map(response => response.json());
}