以下是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 }