Reputation: 3
Getting the following error while hitting the POST API call via postman for the following code.
Requirement : Place an order given the list of <component_id>, and . Consider that an order only implies reducing the current stock of each <component_id> by .
Spring Boot Application is Running fine just getting 400 Bad request in Postman and following in the console.
Error Log
2023-03-01T00:43:19.065+05:30 WARN 53443 --- [nio-8090-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.lang.Long
from Object value (token JsonToken.START_OBJECT
)]
Following are the details of my classes added:
**Controller Class**
@PostMapping("/shop/order")
public ResponseEntity<String> placeOrder(@RequestBody Map<String, Long> orderDetails) {
try {
partService.placeOrder(orderDetails);
return ResponseEntity.ok().body("Order placed successfully!");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to place order");
}
}
Repository class
public interface PartRepository extends CrudRepository < PartDetails, String > {
@Query("SELECT p FROM PartDetails p WHERE p.carModelId = :carModelId")
List < PartDetails > findByCarModelId(String carModelId);
@Query("SELECT p FROM PartDetails p WHERE p.componentId = :componentId")
List < PartDetails > findByComponentId(String componentId);
**Service Class Logic**
@Transactional
public void placeOrder(Map < String, Long > orderDetails) {
for (Map.Entry < String, Long > entry: orderDetails.entrySet()) {
String componentId = entry.getKey();
Long unitInStock = entry.getValue();
List < PartDetails > partStockOptional = partRepository.findByComponentId(componentId);
// PartDetails partStock = partStockOptional.orElseThrow(() -> new ResourceNotFoundException("Part stock not found with component ID: " + componentId));
PartDetails partStock = (PartDetails) partStockOptional;
Long currentUnits = partStock.getUnitInStock();
if (currentUnits < unitInStock) {
throw new RuntimeException("Insufficient units in stock for component ID: " + componentId);
}
partStock.setUnitInStock(currentUnits - unitInStock);
partRepository.save(partStock);
}
}
**Entity Class**
package com.example.ecombackend.Entity;
import jakarta.persistence.*;
@Entity
@Table
public class PartDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "car_model_id")
private String carModelId;
@Column
private String componentName;
@Column
private String componentId;
@Column(name = "unit_in_stock")
private Long unitInStock;
public PartDetails() {}
public PartDetails(String carModelIds, String componentNames, String componentIds, Long unitInStocks) {
carModelId = carModelIds;
componentName = componentNames;
componentId = componentIds;
unitInStock = unitInStocks;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCarModelId() {
return carModelId;
}
public String getComponentName() {
return componentName;
}
public String getComponentId() {
return componentId;
}
public Long getUnitInStock() {
return unitInStock;
}
public void setCarModelId(String carModelId) {
this.carModelId = carModelId;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
}
public void setComponentId(String componentId) {
this.componentId = componentId;
}
public void setUnitInStock(Long unitInStock) {
this.unitInStock = unitInStock;
}
}
`
Upvotes: 0
Views: 14212
Reputation: 31
Not This : { "id":4 }
just give :
4
In the body in postman... I hope this will work
Upvotes: 0
Reputation: 5365
Your body in Postman needs to be { "EA1": 5, "BA1": 3 } not { "orderDetails": { "EA1": 5, "BA1": 3 } }
Upvotes: 0