Spring boot接受参数的两种方式

方式1,使用form表单传输数据,这种方式Content-type默认是"application/x-www-form-urlencoded",注意用postman发送post请求的时候需要选择此类型

@PostMapping(value = "/zte/account", produces = "application/json")
@ResponseBody
public Object addUser(Account account) {
    System.out.println(account);
    accountService.addAccount(account);
    Map map = new HashMap();
    map.put("result", "SUCCESS");
    return map;
}

Spring boot接受参数的两种方式_第1张图片

方式二,使用原生的json数据进行数据的传输,此时,需要将Content-type设置为"application/json",同时服务器端接受json参数的模型前面需要加上注解@RequestBody来完成接送字串到模型的映射转换

@PostMapping(value = "/zte/account", produces = "application/json")
@ResponseBody
public Object addUser(@RequestBody Account account) {
    System.out.println(account);
    accountService.addAccount(account);
    Map map = new HashMap();
    map.put("result", "SUCCESS");
    return map;
}

Spring boot接受参数的两种方式_第2张图片

你可能感兴趣的:(springboot)