Hi I have a REST controller in a SpringBoot application with a number of methods. My GET methods are working fine but my POST method does not work and instead returns an error 405 that the method is not allowed. Any ideas will help; thanks in advance.
@GetMapping("orders")
public List<OrderDto> getOrders() {
List<OrderDto> orderDtos = new ArrayList<>();
// get all the orders using the getOrders method in the OrderService class
List<Order> orders = orderService.getOrders();
// convert each of the returned orders into orderDto
orders.forEach(o->{
orderDtos.add(OrderDto.from(o));
});
return orderDtos;
}
@GetMapping("lineitems")
public List<OrderLineItemDto> getOrderLineItems(){
List<OrderLineItemDto> orderLineItemDtos = new ArrayList<>();
// get line items using the getOrderLineItems service
List<OrderLineItem> orderLineItems = orderLineItemService.getOrderLineItems();
// convert each of the returned order line items into an orderLineItemDto so that the DTO is the one that gets returned to the client
orderLineItems.forEach(ol->{
orderLineItemDtos.add(OrderLineItemDto.from(ol));
});
// return the list of lineItemDtos to the calling client.
return orderLineItemDtos;
}
@GetMapping("lineitems/{id}")
public OrderLineItem getOrderLineItem(@PathVariable Long id) {
return orderLineItemService.getOrderLineItem(id);
}
@PostMapping("order/add")
// @RequestMapping(value = "order",
// consumes = "application/json",
// method = RequestMethod.POST)
public void addOrder(@RequestBody OrderDto orderDto) {
Order order = new Order();
// convert the orderDto into order
order = Order.from(orderDto);
// use the order in the order creation service
orderService.addOrder(order);
}