本次数据请求使用postman, postman下载地址:https://www.getpostman.com/
一、页面跳转
1. 页面跳转
-
@Controller
-
public
class IndexController {
-
-
/**
-
* 进入首页
-
*
-
* @return 首页页面
-
*/
-
@RequestMapping("/")
-
public String index(){
-
return
"/index";
-
}
-
}
2. 请求转发
-
@Controller
-
public
class IndexController {
-
-
/**
-
* 进入首页
-
*
-
* @return 首页页面
-
*/
-
@RequestMapping("/")
-
public String index(){
-
return
"/index";
-
}
-
-
/**
-
* 访问登录页面时,如果已经登录就转发到首页,未登录就进入登录页面
-
*
-
* @return 登录页面
-
*/
-
@RequestMapping("/login")
-
public String forward(){
-
if(
true){
-
return
"forward:/index";
-
}
-
return
"login";
-
}
-
}
3. 重定向
-
@Controller
-
public
class IndexController {
-
-
/**
-
* 进入首页
-
*
-
* @return 首页页面
-
*/
-
@RequestMapping("/")
-
public String index(){
-
return
"/index";
-
}
-
-
/**
-
* 访问登录页面时,如果已经登录就重定向到首页,未登录就进入登录页面
-
*
-
* @return 登录页面
-
*/
-
@RequestMapping("/login")
-
public String redirect(){
-
if(
true){
-
return
"redirect:/index";
-
}
-
return
"login";
-
}
-
}
二、接收表单提交参数
1. 接收简单包装类型
模拟用户登录,接收用户名、密码、验证码参数
-
/**
-
* 提交登陆信息
-
*
-
* @param username 用户名
-
* @param password 密码
-
* @param captcha 验证码
-
* @return 结果
-
*/
-
@PostMapping("/login1")
-
@ResponseBody
-
public Map<String, Object> submitLogin1(String username, String password, String captcha) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"登录成功");
-
resultMap.put(
"username", username);
-
resultMap.put(
"password", password);
-
resultMap.put(
"captcha", captcha);
-
return resultMap;
-
}
2. 通过request获取请求参数
-
/**
-
* 提交登陆信息
-
*
-
* @return 结果
-
*/
-
@PostMapping("/login2")
-
@ResponseBody
-
public Map<String, Object> submitLogin2() {
-
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-
HttpServletRequest request = attributes.getRequest();
-
String username = request.getParameter(
"username");
-
String password = request.getParameter(
"password");
-
String captcha = request.getParameter(
"captcha");
-
-
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"登录成功");
-
resultMap.put(
"username", username);
-
resultMap.put(
"password", password);
-
resultMap.put(
"captcha", captcha);
-
return resultMap;
-
}
3. 接收对象类型
-
@Data
-
public
class LoginUser {
-
-
private String username;
-
-
private String password;
-
-
private String code;
-
}
-
/**
-
* 提交登陆信息
-
*
-
* @return 结果
-
*/
-
@PostMapping("/login3")
-
@ResponseBody
-
public Map<String, Object> submitLogin3(LoginUser loginUser) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"登录成功");
-
resultMap.put(
"username", loginUser.getUsername());
-
resultMap.put(
"password", loginUser.getPassword());
-
resultMap.put(
"captcha", loginUser.getCaptcha());
-
return resultMap;
-
}
4.接收Map类型
-
/**
-
* 提交登陆信息
-
*
-
* @return 结果
-
*/
-
@PostMapping("/login4")
-
@ResponseBody
-
public Map<String, Object> submitLogin4(@RequestParam Map<String, Object> loginUser) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"登录成功");
-
resultMap.put(
"username", loginUser.get(
"username"));
-
resultMap.put(
"password", loginUser.get(
"password"));
-
resultMap.put(
"captcha", loginUser.get(
"captcha"));
-
return resultMap;
-
}
5.接收数组类型
-
/**
-
* 修改角色权限
-
*
-
* @param userId 用户id
-
* @param roleIds 角色id
-
* @return 结果
-
*/
-
@PostMapping("/modifyRole1")
-
@ResponseBody
-
public Map<String, Object> modifyRole1(Integer userId, Integer[] roleIds) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"修改角色权限成功");
-
resultMap.put(
"userId", userId);
-
resultMap.put(
"roleIds", Arrays.toString(roleIds));
-
return resultMap;
-
}
6.接收List类型
-
/**
-
* 修改角色权限
-
*
-
* @param userId 用户id
-
* @param roleIds 角色id
-
* @return 结果
-
*/
-
@PostMapping("/modifyRole2")
-
@ResponseBody
-
public Map<String, Object> modifyRole2(Integer userId, @RequestParam("roleIds") List<Integer> roleIds) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"修改角色权限成功");
-
resultMap.put(
"userId", userId);
-
resultMap.put(
"roleIds", roleIds.toString());
-
return resultMap;
-
}
7.接收Set类型
-
/**
-
* 修改角色权限
-
*
-
* @param userId 用户id
-
* @param roleIds 角色id
-
* @return 结果
-
*/
-
@PostMapping("/modifyRole3")
-
@ResponseBody
-
public Map<String, Object> modifyRole3(Integer userId, @RequestParam("roleIds") Set<String> roleIds) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"修改角色权限成功");
-
resultMap.put(
"userId", userId);
-
resultMap.put(
"roleIds", roleIds.toString());
-
return resultMap;
-
}
8.接收带List参数的实体类
-
@Data
-
public
class ModifyRole {
-
-
private Integer userId;
-
-
private List<String> roleIds;
-
}
-
/**
-
* 修改角色权限
-
*
-
* @param modifyRole 数据
-
* @return 结果
-
*/
-
@PostMapping("/modifyRole4")
-
@ResponseBody
-
public Map<String, Object> modifyRole4(ModifyRole modifyRole) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"修改角色权限成功");
-
resultMap.put(
"modifyRole", modifyRole.toString());
-
return resultMap;
-
}
三、接收JSON参数
1. 接收带List参数的实体类
-
@Data
-
public
class ModifyRole {
-
-
private Integer userId;
-
-
private List<String> roleIds;
-
}
-
/**
-
* 修改角色权限
-
*
-
* @param modifyRole 数据
-
* @return 结果
-
*/
-
@PostMapping("/modifyRole5")
-
@ResponseBody
-
public Map<String, Object> modifyRole5(@RequestBody ModifyRole modifyRole) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"修改角色权限成功");
-
resultMap.put(
"modifyRole", modifyRole.toString());
-
return resultMap;
-
}
2.接收List<Bean>类型
-
@Data
-
public
class SysUser {
-
-
private
String username;
-
-
private
String password;
-
}
-
/**
-
* 批量新增用户
-
*
-
* @param sysUserList 数据集合
-
* @return 结果
-
*/
-
@PostMapping("/batchInsert")
-
@ResponseBody
-
public Map<String, Object> batchInsert(@RequestBody List<SysUser> sysUserList) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"批量新增用户成功");
-
resultMap.put(
"modifyRoleList", sysUserList.toString());
-
return resultMap;
-
}
3.接收Set<Bean>类型
-
/**
-
* 批量新增用户
-
*
-
* @param sysUserSet 数据集合
-
* @return 结果
-
*/
-
@PostMapping("/batchInsert2")
-
@ResponseBody
-
public Map<String, Object> batchInsert2(@RequestBody Set<SysUser> sysUserSet) {
-
Map<String, Object> resultMap =
new HashMap<>(
16);
-
resultMap.put(
"code",
200);
-
resultMap.put(
"msg",
"批量新增用户成功");
-
resultMap.put(
"modifyRoleSet", sysUserSet.toString());
-
return resultMap;
-
}
四、文件上传、下载
1. 文件上传
新建一个文件
-
/**
-
* 上传文件
-
*
-
* @param multipartFile 上传的文件
-
* @return 结果
-
*/
-
@PostMapping("/upload")
-
@ResponseBody
-
public void upload(MultipartFile multipartFile) throws IOException {
-
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-
// 获取文件流
-
InputStream inputStream = multipartFile.getInputStream();
-
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(inputStream, UTF_8.name()))){
-
// 读取文件数据
-
String collect = reader.lines().collect(Collectors.joining());
-
// 直接返回文件数据给前端
-
attributes.getResponse().getWriter().write(collect);
-
}
catch (Exception e){
-
e.printStackTrace();
-
}
-
}
2. 文件下载
-
/**
-
* 文件下载
-
*/
-
@GetMapping("/download")
-
public ResponseEntity<
byte[]> download()
throws IOException {
-
File file = ResourceUtils.getFile(
"classpath:application.yml");
-
-
byte[] body;
-
try (InputStream is =
new FileInputStream(file)){
-
body =
new
byte[is.available()];
-
is.read(body);
-
}
-
-
HttpHeaders headers =
new HttpHeaders();
-
headers.add(
"Content-Disposition",
"attchement;filename=" + file.getName());
-
return
new ResponseEntity<>(body, headers, HttpStatus.OK);
-
}
转载:https://blog.csdn.net/han12398766/article/details/117389740
查看评论