While tring to use json data in java spring boot application, get an bad request message from postmain or application in web. Not able to find root cause of it.
Json Format used in application as follows
{
"stateOfCharge": 30,
"timeSpendAtDest": 30,
"userId": 3745,
"distanceInMeters": 2478.91864342829,
"stationsList": [{
"csId": 50,
"csLat": 17.491125,
"csLng": 78.397686,
"energyLevel": "LEVEL1",
"maxChargeTimeInMins": 720,
"outPutRateInKw": 2,
"price": 0.8,
"distance": 126.31235091469274
}, {
"csId": 52,
"csLat": 17.491168,
"csLng": 78.398331,
"energyLevel": "LEVEL2",
"maxChargeTimeInMins": 480,
"outPutRateInKw": 19,
"price": 2.5,
"distance": 85.98535639001425
}, {
"csId": 50,
"csLat": 17.491125,
"csLng": 78.397686,
"energyLevel": "DCFAST",
"maxChargeTimeInMins": 30,
"outPutRateInKw": 350,
"price": 15,
"distance": 126.31235091469274
}]
}
Controller written like this and getting 400 bad requeset responce in java
@PostMapping("/stations")
@ApiOperation(value = "Get charging stations around 400 radius from the charging location.")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success"),
})
public void findChargingStations(
@ApiParam(value = "stateOfCharge") @NotNull @RequestParam Integer stateOfCharge,
@ApiParam(value = "timeSpendAtDest") @NotNull @RequestParam Integer timeSpendAtDest,
@ApiParam(value = "userId") @NotNull @RequestParam Integer userId,
@ApiParam(value = "distanceInMeters") @NotNull @RequestParam Integer distanceInMeters,
@RequestBody(value = "stationsList") @NotNull @RequestParam FindStations stationsList
) throws Exception {
this.findChargingStationsService.getFilteredStations(stateOfCharge, timeSpendAtDest, userId, distanceInMeters,stationsList);
return;
}
FindStations is any interface for mapping fields
public interface FindStations {
int getCsId();
double getCsLat();
double getCsLng();
float getPrice();
String getEnergyLevel();
int getMaxChargeTimeInMins();
int getOutPutRateInKw();
int getDistance();
}
Can one help to sove the issue
CodePudding user response:
You are sending some complex data to the backend. You may want to send it in the request body, not as request parameters.
You can use @RequestBody
for that.
First, let's create some DTO classes for handling the data sent from the frontend:
public class StationRequest {
private int stateOfCharge;
private int timeSpendAtDest;
private int userId;
private double distanceInMeters;
private List<Station> stationsList;
public int getStateOfCharge() {
return stateOfCharge;
}
public void setStateOfCharge(int stateOfCharge) {
this.stateOfCharge = stateOfCharge;
}
// Other getters and setters omitted for readability. You may want to consider project Lombok
}
public class Station {
private int csId;
private double csLat;
private double csLng;
private String energyLevel;
private int maxChargeTimeInMins;
private int outPutRateInKw;
private double price;
private double distance;
// Getters and setter omitted.
}
Let's create the controller:
@PostMapping("/stations")
public void findChargingStations(@RequestBody StationRequest stationRequest) {
// Do some business logic
}