在我的角度应用程序中,我正在尝试向spring restful API发出http post请求..但我无法成功..我在浏览器控制台中没有收到任何错误响应..
我的角度代码是,
addToCart(productId: number, quantity: number) {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
console.log("--------order.service.ts----------addToCart()-------productId:"+productId+":------quantity:"+quantity);
this.http.post('http://localhost:8080/order/addtocart',
'{ "dealerId": 9, "createdBy": "-1", "productId": productId, "quantity": quantity}',
{headers: headers})
.pipe(catchError(this.errorHandlerService.handleError));
}
Spring restful API:
package com.wocs.rest.controller;
import java.io.IOException;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wocs.services.common.ServiceException;
import com.wocs.services.order.iface.OrderServiceIface;
import com.wocs.services.userrole.model.User;
@RestController()
@RequestMapping("/order")
public class OrderController {
static Logger logger = Logger.getLogger(OrderController.class);
@Autowired
private OrderServiceIface orderService;
public void setOrderService(OrderServiceIface orderService) {
this.orderService = orderService;
}
@RequestMapping(value = "/addtocart", method = RequestMethod.POST, consumes = "text/plain")
public void addToCart(@RequestBody String stringRequestBody) throws JsonParseException, JsonMappingException, IOException, ServiceException
{
logger.info("addtocart:"+stringRequestBody);
Map<String, Object> jsonMap = new ObjectMapper().readValue(stringRequestBody,
new TypeReference<Map<String,Object>>(){});
orderService.addToCart((Integer)jsonMap.get("dealerId"), (String) jsonMap.get("createdBy"), (Integer)jsonMap.get("productId"), (Integer)jsonMap.get("quantity"));
}
}
浏览器控制台:
任何帮助将不胜感激..提前致谢..
答案 0 :(得分:1)
您没有订阅this.http.post(...)
函数,该函数返回一个observable。
在您订阅它之前,observable不执行任何操作,因此您的代码应为:
addToCart(productId: number, quantity: number) {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
this.http.post('http://localhost:8080/order/addtocart',
'{ "dealerId": 9, "createdBy": "-1", "productId": productId, "quantity": quantity}',
{headers: headers})
.pipe(catchError(this.errorHandlerService.handleError))
.subscribe(data => {
// Handle the updated data here.
console.log(data);
});
}
或者,如果您获得可以直接在视图中使用的任何数据,您可以使用async pipe来处理订阅。
答案 1 :(得分:1)
您错过了订阅。如果您已在服务中声明addToCart
并希望在组件修改代码中处理API响应:
<强>服务强>
addToCart(productId: number, quantity: number) {
let data = {
"dealerId": 9,
"createdBy": "-1",
"productId": productId,
"quantity": quantity
}
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
console.log("--------order.service.ts----------addToCart()-------productId:"+productId+":------quantity:"+quantity);
return this.http.post('http://localhost:8080/order/addtocart',
JSON.stringify(data),
{headers: headers})
.pipe(catchError(this.errorHandlerService.handleError));
}
组件 订阅服务方法
this.service.addToCart(2, 4).subscribe(res => {
console.log(res); // Response from API
})