如何在 java 格式的 json 控制器中编写 Web 服务
How to write web service in contrroller for this formated json in java
尝试在 java spring 启动应用程序中使用 json 数据时,从 web 中的 postmain 或应用程序收到错误的请求消息。找不到根本原因。
Json申请中使用的格式如下
{
"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
}]
}
这样写的控制器在 java
中收到 400 个错误的请求响应
@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 是映射字段的任何接口
public interface FindStations {
int getCsId();
double getCsLat();
double getCsLng();
float getPrice();
String getEnergyLevel();
int getMaxChargeTimeInMins();
int getOutPutRateInKw();
int getDistance();
}
谁能帮忙解决问题
您正在向后端发送一些复杂的数据。您可能希望在请求正文中发送它,而不是作为请求参数。
您可以使用 @RequestBody
。
首先,让我们创建一些 DTO 类 来处理从前端发送的数据:
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.
}
让我们创建控制器:
@PostMapping("/stations")
public void findChargingStations(@RequestBody StationRequest stationRequest) {
// Do some business logic
}
尝试在 java spring 启动应用程序中使用 json 数据时,从 web 中的 postmain 或应用程序收到错误的请求消息。找不到根本原因。
Json申请中使用的格式如下
{
"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
}]
}
这样写的控制器在 java
中收到 400 个错误的请求响应@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 是映射字段的任何接口
public interface FindStations {
int getCsId();
double getCsLat();
double getCsLng();
float getPrice();
String getEnergyLevel();
int getMaxChargeTimeInMins();
int getOutPutRateInKw();
int getDistance();
}
谁能帮忙解决问题
您正在向后端发送一些复杂的数据。您可能希望在请求正文中发送它,而不是作为请求参数。
您可以使用 @RequestBody
。
首先,让我们创建一些 DTO 类 来处理从前端发送的数据:
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.
}
让我们创建控制器:
@PostMapping("/stations")
public void findChargingStations(@RequestBody StationRequest stationRequest) {
// Do some business logic
}