SpringMVC:3.SpringMVC中的@PostMapping and @RequestBody 示例

在HTTP Post 请求中发送JSON数据

URL:

http://localhost:8080/api/users

JSON:

{
  "firstName":"Sergey",
  "lastName":"Kargopolov",
  "email":"test9@test.com",
  "password":"123"
}

使用CURL发送HTTP POST请求

CURL:

curl -X POST \
  http://localhost:8080/api/users \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
  "firstName":"Sergey",
  "lastName":"Kargopolov",
  "email":"test9@test.com",
  "password":"123"
}'

@PostMapping接收HTTP POST请求数据

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    UserService userService;
 
    @PostMapping
    public UserRest createUser(@RequestBody UserDetailsRequestModel requestUserDetails) {
        UserRest returnValue = new UserRest();
                
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(requestUserDetails, userDto);
        UserDto createdUser = userService.createUser(userDto);
        BeanUtils.copyProperties(createdUser, returnValue);
        return returnValue;
    }
}

为了能够将以HTTP发送的JSON转换为可在我们的应用程序中使用的Java对象,需要对方法参数使用@RequestBody注解。
注意,@RequestBody注解如何用于标记Spring框架将JSON文档转换为的方法参数对象。

createUser(@RequestBody UserDetailsRequestModel requestUserDetails)

现在,这里唯一缺少的细节是我们没有UserDetailsRequestModel Java类,Spring框架会将传入的JSON映射到Java类中。 Spring将为我们创建UserDetailsRequestModel Java类的实例,并将使用JSON中指定的值设置对象属性。

转换 JSON对象到自定义类

当我们使用@RequestBody注解方法参数时,就是在告诉框架将HTTP请求的参数的JSON或XML有效负载转换为给定类型的对象。 创建一个Java类,将JSON或XML内容转换为该Java类。

UserDetailsRequestModel

类中的属性JSON属性完全匹配。

JSON:

{
  "firstName":"Sergey",
  "lastName":"Kargopolov",
  "email":"test9@test.com",
  "password":"123"
}

Java:

public class UserDetailsRequestModel {
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
    
}

选择接收的数据为JSON或者XML

为了使方法使用@PostMapping注解使用以下注解在JSON和XML中接受@RequestBody:

@PostMapping(
        consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}
)
public UserRest createUser(@RequestBody UserDetailsRequestModel requestUserDetails) {
    UserRest returnValue = new UserRest();
            
    UserDto userDto = new UserDto();
    BeanUtils.copyProperties(requestUserDetails, userDto);
    UserDto createdUser = userService.createUser(userDto);
    BeanUtils.copyProperties(createdUser, returnValue);
    return returnValue;
}

返回JSON或者XML类型的值

HTTP Header设置使得返回值类型为xml:

accept: application/xml

CURL:

curl -X POST \
  http://localhost:8080/api/users \
  -H 'accept: application/xml' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
  "firstName":"Sergey",
  "lastName":"Kargopolov",
  "email":"test9@test.com",
  "password":"123"
}'
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丷丩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值