SpringBoot 请求参数接收 SpringBoot请求参数接收方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot 请求参数接收 SpringBoot请求参数接收方式

aa东   2021-04-02 我要评论

application/json接收

/** 
 * 参数不可为空,可为{}
 * userDto中的属性 非必填 
 */
@RequestMapping("/hello5") 
public String hello5(@RequestBody UserDto userDto) { 
  return userDto.getName() + "," \+ userDto.getAge(); 
}

x-www-form-urlencoded、?拼接、form-data接收

@RequestMapping("/hello1") 
public String hello1(@RequestParam("name") String name) { 
  return name; 
} 
 
@RequestMapping("/hello2") 
public UserDto hello2(@RequestHeader("name") String name, @RequestHeader("age") Integer age) { 
  return new UserDto(name, age); 
} 
 
/** 
 * @param name 非必填 
 */
@RequestMapping("/hello3") 
public String hello3(String name) { 
  return name; 
} 
 
/** 
 * userDto中的属性 非必填 
 */
@RequestMapping("/hello4") 
public String hello4(UserDto userDto) { 
  return userDto.getName() + "," \+ userDto.getAge(); 
}

UserDto

public class UserDto { 
 
  private String name; 
 
  private Integer age; 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public Integer getAge() { 
    return age; 
  } 
 
  public void setAge(Integer age) { 
    this.age = age; 
  } 
}
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们