springBoot中Contoller的注解用法(文件上传、下载、路径参数等)

    以下是springBoot中Contoller层的各种情况注解使用。

    @RestController

    @RequestMapping

    @PostMapping

    @RequestBody

    @ResponseBody

    @PathParam

    @RequestParam

  1 package com.cheng2839.controller;
  2 
  3 import org.apache.commons.lang.CharEncoding;
  4 import org.springframework.web.bind.annotation.PathVariable;
  5 import org.springframework.web.bind.annotation.PostMapping;
  6 import org.springframework.web.bind.annotation.RequestBody;
  7 import org.springframework.web.bind.annotation.RequestMapping;
  8 import org.springframework.web.bind.annotation.RequestMethod;
  9 import org.springframework.web.bind.annotation.RequestParam;
 10 import org.springframework.web.bind.annotation.ResponseBody;
 11 import org.springframework.web.bind.annotation.RestController;
 12 import org.springframework.web.multipart.MultipartFile;
 13 
 14 import javax.servlet.http.Cookie;
 15 import javax.servlet.http.HttpServletRequest;
 16 import javax.servlet.http.HttpServletResponse;
 17 import javax.websocket.server.PathParam;
 18 import java.io.File;
 19 import java.io.FileOutputStream;
 20 import java.io.IOException;
 21 import java.io.InputStream;
 22 import java.io.OutputStream;
 23 import java.io.PrintWriter;
 24 import java.util.Arrays;
 25 
 26 /**
 27  * @RestController 表示是一个Controller,以json方式输出
 28  * @RequestMapping 表示路径前缀
 29  */
 30 @RestController
 31 @RequestMapping("/root")
 32 public class MyTestController {
 33 
 34     /**
 35      * 普通的使用包装对象作为入参,使用@RequestBody
 36      * @PostMapping 表示Post请求
 37      *
 38      * @param testVo
 39      * @return
 40      */
 41     @PostMapping(value = "/test1", produces = "application/json")
 42     public String test1(@RequestBody TestVo testVo) {
 43         try {
 44             return "success";
 45         } catch (Exception e) {
 46             return "error";
 47         }
 48     }
 49 
 50     /**
 51      * @ResponseBody 表示响应以json对象输出,和@Controller同时使用,由于此类是用@RestController修饰,所以可以省略@ResponseBody了
 52      * @RequestMapping 可以指定请求字符集、请求方式等
 53      * @PathVariable 表示path后的/带的路径参数
 54      * @param id
 55      * @return
 56      */
 57     //@ResponseBody
 58     @RequestMapping(path = "/test2/{id}", method = RequestMethod.POST, produces = "application/json")
 59     public String test2(@PathVariable(name = "id") String id) {
 60         return id;
 61     }
 62 
 63     /**
 64      * @PathParam 表示请求地址后?中带的参数值
 65      * @param token
 66      * @return
 67      */
 68     @RequestMapping(path = "/test3", method = RequestMethod.GET, produces = "application/json")
 69     public String test3(@PathParam(value = "token") String token) {
 70         return token;
 71     }
 72 
 73     /**
 74      * 适用于下载文件时用
 75      * @param request
 76      * @param response
 77      * @return
 78      * @throws IOException
 79      */
 80     @RequestMapping(path = "/test4", method = RequestMethod.GET, produces = "application/json")
 81     public String test4(HttpServletRequest request, HttpServletResponse response) throws IOException {
 82         String token = request.getHeader("token");  //获取header内容
 83         boolean type = (Boolean) request.getSession().getAttribute("type"); //获取session中值
 84         Cookie[] cookies = request.getCookies();  //获取cookie内容
 85 
 86         //输出数据
 87         String stringData = new String("token="+token+";cookies="+ Arrays.toString(cookies));
 88         byte[] bytes = stringData.getBytes(CharEncoding.UTF_8);
 89 
 90         //以流的方式输出(下载)
 91         if (type) {
 92             OutputStream os = response.getOutputStream();
 93             os.write(bytes);
 94             os.flush();
 95             os.close();
 96         } else {
 97             //以文字的方式输出
 98             PrintWriter writer = response.getWriter();
 99             writer.print(stringData); //响应值
100         }
101         return "success";
102     }
103 
104 
105     /**
106      * 适用于上传文件时用(在form-data中放入文件,名称为file)
107      * @RequestParam 表示通过form表单提交的参数对象
108      * @return
109      * @throws IOException
110      */
111     @RequestMapping(path = "/test5", method = RequestMethod.GET, produces = "application/x-www-form-urlencoded")
112     public String test5(@RequestParam(value = "file", required = false) MultipartFile multipartFile) throws IOException{
113         InputStream inputStream = multipartFile.getInputStream();
114         File cloudFile = new File("/mnt/cheng2839/files/"+System.currentTimeMillis()+multipartFile.getOriginalFilename());
115         FileOutputStream fileOutputStream = new FileOutputStream(cloudFile);
116         byte[] buf = new byte[1024];
117         int len = -1;
118         while ((len=inputStream.read(buf))>0) {
119             fileOutputStream.write(buf, 0, len);
120         }
121         fileOutputStream.flush();
122         fileOutputStream.close();
123         inputStream.close();
124         return "success";
125     }
126 
127 
128     class TestVo {
129         private String name;
130         private String account;
131         // getter and setter functions
132     }
133 
134 }
____________________________特此,勉励____________________________
本文作者cheng2839
本文链接https://www.cnblogs.com/cheng2839
关于博主:评论和私信会在第一时间回复。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
原文地址:https://www.cnblogs.com/cheng2839/p/12660411.html