Web Services 如何在控制器中用java编写此格式化json的web服务

u4vypkhs  于 2022-11-15  发布在  Java
关注(0)|答案(1)|浏览(158)

特灵在java spring Boot 应用程序中使用json数据时,从postmain或Web中的应用程序收到错误的请求消息。无法找到根本原因。
应用程序中使用的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是用于Map字段的任意接口

public interface FindStations {
    int getCsId();
    double getCsLat();
    double getCsLng();
    float getPrice();
    String getEnergyLevel();
    int getMaxChargeTimeInMins();
    int getOutPutRateInKw();
    int getDistance();
}

有人能帮忙解决这个问题吗

tyky79it

tyky79it1#

您正在向后端发送一些复杂数据。您可能希望在请求正文中发送这些数据,而不是将其作为请求参数发送。
您可以使用@RequestBody来实现这一点。
首先,让我们创建一些DTO类来处理从前端发送的数据:
第一个
让我们创建控制器:

@PostMapping("/stations")
public void findChargingStations(@RequestBody StationRequest stationRequest) {
    // Do some business logic
}

相关问题