Spring REST response当我们有两个对象时如何返回响应

Spring REST response当我们有两个对象时如何返回响应,spring,rest,web-services,spring-mvc,spring-restcontroller,Spring,Rest,Web Services,Spring Mvc,Spring Restcontroller,1。例如,UserProfile有三个属性:名称、出生日期、年龄 2。第二类是UserProfileResponse,它只有“id” 提前感谢。您可以调用createUserProfileData方法并从中返回新插入对象的id 在createUserProfileData方法中,可以调用存储库的saveAndFlush方法,该方法将保存userProfile对象 这将返回新插入对象的id 最后,您的代码如下所示: public ResponseEntity<UserProfileRespo

1。例如,UserProfile有三个属性:名称、出生日期、年龄
2。第二类是UserProfileResponse,它只有“id”


提前感谢。

您可以调用
createUserProfileData
方法并从中返回新插入对象的id

createUserProfileData
方法中,可以调用存储库的
saveAndFlush
方法,该方法将保存
userProfile
对象

这将返回新插入对象的id

最后,您的代码如下所示:

public ResponseEntity<UserProfileResponse> createUserProfile(@RequestBody UserProfile userProfile)
{
      UserProfileResponse userProfileResponse = new UserProfileResponse();
      int id = createUserProfileData(userProfile)
      userProfileResponse.setId(id) 
      return new ResponseEntity<UserProfileResponse>(userProfileResponse,HTTPStatus.OK);  
}
public ResponseEntity createUserProfile(@RequestBody UserProfile UserProfile)
{
UserProfileResponse UserProfileResponse=新的UserProfileResponse();
int id=createUserProfileData(userProfile)
userProfileResponse.setId(id)
返回新的ResponseEntity(userProfileResponse,HTTPStatus.OK);
}

您可以调用
createUserProfileData
方法并从中返回新插入对象的id

createUserProfileData
方法中,可以调用存储库的
saveAndFlush
方法,该方法将保存
userProfile
对象

这将返回新插入对象的id

最后,您的代码如下所示:

public ResponseEntity<UserProfileResponse> createUserProfile(@RequestBody UserProfile userProfile)
{
      UserProfileResponse userProfileResponse = new UserProfileResponse();
      int id = createUserProfileData(userProfile)
      userProfileResponse.setId(id) 
      return new ResponseEntity<UserProfileResponse>(userProfileResponse,HTTPStatus.OK);  
}
public ResponseEntity createUserProfile(@RequestBody UserProfile UserProfile)
{
UserProfileResponse UserProfileResponse=新的UserProfileResponse();
int id=createUserProfileData(userProfile)
userProfileResponse.setId(id)
返回新的ResponseEntity(userProfileResponse,HTTPStatus.OK);
}

如果您想从RequestBody用户配置文件中获取一个实际不包含的值。很抱歉,这是不可能的

我们一次只能收到一个requestBody,因此我们需要使用其他一些方法来收集信息。还有一些其他解决方案:

  • 使用@PathVariable从url获取ID
  • 使用@RequestParam从RequestParam获取Id
  • 将名为Id的新字段添加到用户配置文件中
  • 使用另一种可以获取Id的方法,这取决于您如何持久化或生成Id
在你的情况下,我不确定你要用这个身份证做什么

如果“createUserProfileData”方法意味着您需要首先提供一个id以实现持久性

嗯,我不知道您使用的是哪种数据库和哪种框架。据我所知,大多数框架和数据库都能够自动生成id。但是如果您坚持自己生成id,我建议您使用UUID

如果“createUserProfileData”方法按字面意思将UserProfile保存到数据库中,并且id是由数据库本身生成的,那么您只需这样做,并将id表示您刚才保存到UserProfileResponse的记录


至于如何获取表示您刚刚保存的记录的id?这取决于您使用的框架以及代码的编写方式。

如果您想从RequestBody UserProfile中获取一个实际不包含的值。很抱歉,这是不可能的

我们一次只能收到一个requestBody,因此我们需要使用其他一些方法来收集信息。还有一些其他解决方案:

  • 使用@PathVariable从url获取ID
  • 使用@RequestParam从RequestParam获取Id
  • 将名为Id的新字段添加到用户配置文件中
  • 使用另一种可以获取Id的方法,这取决于您如何持久化或生成Id
在你的情况下,我不确定你要用这个身份证做什么

如果“createUserProfileData”方法意味着您需要首先提供一个id以实现持久性

嗯,我不知道您使用的是哪种数据库和哪种框架。据我所知,大多数框架和数据库都能够自动生成id。但是如果您坚持自己生成id,我建议您使用UUID

如果“createUserProfileData”方法按字面意思将UserProfile保存到数据库中,并且id是由数据库本身生成的,那么您只需这样做,并将id表示您刚才保存到UserProfileResponse的记录


至于如何获取表示您刚才保存的记录的id?这取决于您使用的框架以及代码的编写方式。

userProfileResponse.setId(userProfileResponse.getId())
没有任何意义。这不会改变任何反应。我们不知道,因为我们不知道响应中的ID应该是什么。如果该方法创建的是用户配置文件的ID,则假设创建用户配置文件生成其ID,然后在创建用户配置文件后将其从用户配置文件中取出。
userProfileResponse.setId(userProfileResponse.getId())
没有任何意义。这不会改变任何反应。我们不知道,因为我们不知道响应中的ID应该是什么。如果该方法创建的是用户配置文件的ID,则假设创建用户配置文件生成其ID,然后在创建用户配置文件后将其从用户配置文件中取出。
public ResponseEntity<UserProfileResponse> createUserProfile(@RequestBody UserProfile userProfile)
{
      UserProfileResponse userProfileResponse = new UserProfileResponse();
      int id = createUserProfileData(userProfile)
      userProfileResponse.setId(id) 
      return new ResponseEntity<UserProfileResponse>(userProfileResponse,HTTPStatus.OK);  
}