For Three days I was struck on running my Cart endpoints.Everything seems correct.
But the Error on running the endpoint was :
{
"success": false,
"message": "The given id must not be null",
"data": null
}
Enter fullscreen mode Exit fullscreen mode
Somewhere the ID was becoming null before it reaches the repository even when i was passing the Id.
Project Explanation
Let me first clear the endpoints and scenario to you.
I am Building an Full Fledged AI-aided E-com project using spring boot,OpenAI Models and BLIP-CLIP models for the Image-to-Text and Text-to-Image processing.
Right now,I am creating the Entities,Repository,Services and Controllers for basic working of the website.
I am also using DTO’s for the structured data transfer.
The problem started with Cart endpoints,my ‘addToCart’ service was not actually adding the products into the cart even when I was passing the values I was getting the id to be null.
Cart Endpoint
/cart/addToCartRequest body
application/json
Example Value
Schema
{
“userId”: 9007199254740991,
“productId”: 9007199254740991,
“quantity”: 1073741824
}Response
{
“success”: boolean,
“message”: “string”,
“data”: “string”
}
Error resolution
I queried all the sources and I was getting more confused,Nothing Clearly was error there in my code yet the endpoints were not responsing.
checklist :
- Fix Mapping Annotations
- Use
@RequestBodyfor JSON data sent in a POST or PUT body. - Use
@RequestParamfor query parameters in the URL (like ?name=test). - Use
@PathVariablefor variables built directly into the URL path (like /users/{id}).
- Check Data Binding and Names
Ensure your JSON property names match your Java class variable names exactly.
Verify that your DTO class has getter and setter methods (or use Lombok
@Dataor@Getter/@Setter), which Jackson needs to read the data.Check that the Content-Type header in your client request is set to application/json when sending a JSON body.
Let me explain you these by example
Lets take one example,we are creating a cart service in an e-com project and we have this controller for add to cart service.
ResponseEntity<ApiResponse<String>> addToCart(@RequestBody CartRequest request){
String status = service.addToCart(request);
ApiResponse<String> response =
new ApiResponse<>(
true,
"Added",
status
);
return ResponseEntity.ok(response);
}
Enter fullscreen mode Exit fullscreen mode
With the response DTO as cartRequest as follows:
public class CartRequest {
@NotNull
@Positive
private Long UserId;
@NotNull
@Positive
private Long ProductId;
@NotNull
@Positive
@Min(1)
private Integer Quantity;
}
Enter fullscreen mode Exit fullscreen mode
Everything is in sync right?
Congrats! you have hit the error.
Remember?
Ensure your JSON property names match your Java class variable names exactly.
This is the error I was struck into, this the ‘u’ replaced with ‘U’.Notice the Capital letter at the beginning of the the DTO variables.
public class CartRequest {
@NotNull
@Positive
private Long ~~UserId;~~
@NotNull
@Positive
private Long ~~ProductId;~~
@NotNull
@Positive
@Min(1)
private Integer ~~Quantity;~~
}
Enter fullscreen mode Exit fullscreen mode
What I learned
Conventions Matters and not every advice and error handling will be provided to you as such.
keep Things in mind and follow conventions for devlopment.