0

我想通过 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 参数都是空的。只是说我看到了一些类似的问题,可能的解决方案之一是缺少 getter/settters,但我有它们。也许这个错误是因为我的控制器,但我很确定这个错误是因为错误的 JSON 格式。有人能帮我吗?

4

1 回答 1

1

@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);
    }
于 2020-06-17T00:35:52.113 回答