使用 Postman 上的 PUT 方法发送到 RestController 的两个 @RequestBody 对象的空值?
Null values for both @RequestBody Objects sent with PUT method on Postman to RestController?
我想通过 PUT 方法发送两个不同的对象,第一个对象是 UserDTO,第二个是 AdsDTO。但由于某种原因,它们在服务实现中都是空的。这是我的 json:
{
"userDTO" :
{
"aboutUs": "xxx",
"address": "xxx",
"businessType": "xxx",
"city": "xx",
"company": "xxxx",
"companyImage": [
"xxx","xxx"
],
"credit": "xx",
"dateOfBirth": "xxx",
"email": "xxx",
"location": "xxx",
"mobile": "xxx",
"name": "xxx",
"phone": "xxx",
"region": "xxx",
"roleName": "xxx",
"surname": "xxx",
"userName": "xxx",
"visible": "xxx",
"website": "xxx"
},
"adsDTO" : {
"adsGroupId": "xx",
"adsSubGroupId": "xx",
"adsType": "xxx",
"description": "xxx",
"image": [
"xxx", "xxx"
],
"price": "xxx",
"productName": "xxx"}
}
还有我的控制器:
@PutMapping("users/favourites")
public ResponseEntity<UserDTO> updateUserFavourites(@RequestHeader("Authorization")String token, @RequestBody UserDTO userDTO, AdsDTO adsDTO) throws NotFoundException, ForbiddenException, BadRequestException{
return new ResponseEntity<UserDTO>(jwtUserServiceImplement.updateUserFavourites(userDTO, token, adsDTO), HttpStatus.OK);
}
当然,这个xxx是隐藏值:)。当我在 Postman 和调试应用程序上点击 PUT 请求时,我看到我的两个 @RequestBody 参数都是空的。只是说我看到了一些类似的问题,并且缺少一种可能的解决方案 getters/settters 但我有。也许这个错误是因为我的控制器,但我很确定这个错误是因为错误的 JSON 格式。有人可以帮助我吗?
@RequestBody 不能有两个对象。但是您可以创建一个封装您的两个 DTO 的请求对象并使用它:
public class UserAdsRequest {
private userDTO;
private adsDTO;
// getters and setters
}
PutMapping("users/favourites")
public ResponseEntity<UserDTO> updateUserFavourites(@RequestHeader("Authorization")String token, @RequestBody UserAdsRequest userAdsRequest) throws NotFoundException, ForbiddenException, BadRequestException{
return new ResponseEntity<UserDTO>(jwtUserServiceImplement.updateUserFavourites(userAdsRequest.getUserDTO(), token, userAdsRequest.getAdsDTO()), HttpStatus.OK);
}
我想通过 PUT 方法发送两个不同的对象,第一个对象是 UserDTO,第二个是 AdsDTO。但由于某种原因,它们在服务实现中都是空的。这是我的 json:
{
"userDTO" :
{
"aboutUs": "xxx",
"address": "xxx",
"businessType": "xxx",
"city": "xx",
"company": "xxxx",
"companyImage": [
"xxx","xxx"
],
"credit": "xx",
"dateOfBirth": "xxx",
"email": "xxx",
"location": "xxx",
"mobile": "xxx",
"name": "xxx",
"phone": "xxx",
"region": "xxx",
"roleName": "xxx",
"surname": "xxx",
"userName": "xxx",
"visible": "xxx",
"website": "xxx"
},
"adsDTO" : {
"adsGroupId": "xx",
"adsSubGroupId": "xx",
"adsType": "xxx",
"description": "xxx",
"image": [
"xxx", "xxx"
],
"price": "xxx",
"productName": "xxx"}
}
还有我的控制器:
@PutMapping("users/favourites")
public ResponseEntity<UserDTO> updateUserFavourites(@RequestHeader("Authorization")String token, @RequestBody UserDTO userDTO, AdsDTO adsDTO) throws NotFoundException, ForbiddenException, BadRequestException{
return new ResponseEntity<UserDTO>(jwtUserServiceImplement.updateUserFavourites(userDTO, token, adsDTO), HttpStatus.OK);
}
当然,这个xxx是隐藏值:)。当我在 Postman 和调试应用程序上点击 PUT 请求时,我看到我的两个 @RequestBody 参数都是空的。只是说我看到了一些类似的问题,并且缺少一种可能的解决方案 getters/settters 但我有。也许这个错误是因为我的控制器,但我很确定这个错误是因为错误的 JSON 格式。有人可以帮助我吗?
@RequestBody 不能有两个对象。但是您可以创建一个封装您的两个 DTO 的请求对象并使用它:
public class UserAdsRequest {
private userDTO;
private adsDTO;
// getters and setters
}
PutMapping("users/favourites")
public ResponseEntity<UserDTO> updateUserFavourites(@RequestHeader("Authorization")String token, @RequestBody UserAdsRequest userAdsRequest) throws NotFoundException, ForbiddenException, BadRequestException{
return new ResponseEntity<UserDTO>(jwtUserServiceImplement.updateUserFavourites(userAdsRequest.getUserDTO(), token, userAdsRequest.getAdsDTO()), HttpStatus.OK);
}