0

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);
        
    }

Response when I invoke the REST service via postman

Hungu
  • 1
  • 1
    Looking at your postman response, it seems the request is being interpreted as get, even though post is selected. can you please show us the header you are adding? try executing the request using curl – marc Apr 29 '21 at 11:30
  • Thank you for your assistance. Turned out the the issue was with a security configuration problem as described in this post here https://stackoverflow.com/a/52662997/9549044 – Hungu May 01 '21 at 11:17

0 Answers0