關于ResponseEntity類和HttpEntity及跨平臺路徑問題
1. 簡介
使用spring時,達到同一目的通常有很多方法,對處理http響應也是一樣。本文我們學習如何通過ResponseEntity設置http相應內容、狀態(tài)以及頭信息。
ResponseEntity是HttpEntity的擴展,添加一個HttpStatus狀態(tài)代碼。在RestTemplate和@Controller方法中使用。
ResponseEntity標識整個http相應:狀態(tài)碼、頭部信息以及相應體內容。因此我們可以使用其對http響應實現(xiàn)完整配置
理解:
- ResponseEntity的優(yōu)先級高于@ResponseBody。在不是ResponseEntity的情況下才去檢查有沒有@ResponseBody注解。如果響應類型是ResponseEntity可以不寫@ResponseBody注解,寫了也沒有關系。
- ResponseEntity 是在 org.springframework.http.HttpEntity 的基礎上添加了http status code(http狀態(tài)碼),用于RestTemplate以及@Controller的HandlerMethod。它在Controller中或者用于服務端響應時,作用是和@ResponseStatus與@ResponseBody結合起來的功能一樣的。用于RestTemplate時,它是接收服務端返回的http status code 和 result的。
- 簡單粗暴的講 @ResponseBody可以直接返回Json結果, @ResponseEntity不僅可以返回json結果,還可以定義返回的HttpHeaders和HttpStatus
2. 使用
2.1 RestTemplate
ResponseEntity<String> entity = template.getForEntity("https://hello.com", String.class); String body = entity.getBody(); MediaType contentType = entity.getHeaders().getContentType(); HttpStatus statusCode = entity.getStatusCode();
2.2 Controller
@RestController @RequestMapping("/demo") public class DemoController { @GetMapping("/get") public ResponseEntity<String> get() { return ResponseEntity.ok("hello"); } }
2.3 指定響應狀態(tài)碼
靜態(tài)方式
@GetMapping("/get") public ResponseEntity<String> get() { return ResponseEntity.status(HttpStatus.LOCKED).body("服務不可用"); }
也可以通過非靜態(tài)方式構建
@GetMapping("/get") public ResponseEntity<String> get() { ResponseEntity responseEntity = new ResponseEntity("服務不可用", HttpStatus.LOCKED); return responseEntity; }
2.4 自定義響應頭
@GetMapping("/get") public ResponseEntity<String> get() { return ResponseEntity.ok() .header("Custom-Header", "lisa") .body("Custom header set"); }
2.5 下載文件
@GetMapping("/download") public ResponseEntity<byte[]> get() throws IOException { // 你放的文件路徑 String filePath = "C:" + File.separator + "Users" + File.separator + "admin002" + File.separator + "Desktop" + File.separator + "work" + File.separator + "img"; File file = new File(filePath + File.separator + "java.png"); // 設置一個head HttpHeaders headers = new HttpHeaders(); // 文件的屬性,也就是文件叫什么 headers.setContentDispositionFormData("attachment", "1.png"); // 內容是字節(jié)流 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 開始下載 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); }
2.6 直接操作HttpServletResponse
Spring 也允許我們直接 javax.servlet.http.HttpServletResponse 對象;只需要申明其作為方法參數(shù):
@GetMapping("/get") public void get(HttpServletResponse response) throws IOException { response.setHeader("Custom-Header", "lisa"); response.setStatus(200); response.getWriter().println("Hello World!"); }
但需要說明,既然spring已經提供底層實現(xiàn)的抽象和附件功能,當然不建議直接操作response。
3. 擴展(跨平臺路徑問題)
File.separator
:系統(tǒng)相關的默認名稱分隔符,為方便起見表示為字符串。該字符串只包含一個字符,即separatorCharseparatorChar
:系統(tǒng)依賴的默認名稱分隔符。這個字段被初始化為包含系統(tǒng)屬性file.separator值的第一個字符。在UNIX系統(tǒng)上,這個字段的值是’\‘;在Microsoft Windows系統(tǒng)上它是’\\’
注意:如果要考慮跨平臺,則最好使用File.separator標識路徑分隔符,不要直接用字符串’\\'來表示
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java之NoClassDefFoundError的原因及分析
在Java開發(fā)中,經常會遇到ClassNotFoundException和NoClassDefFoundError異常,ClassNotFoundException發(fā)生在編譯時JVM無法找到類,而NoClassDefFoundError則發(fā)生在運行時JVM無法加載類,這兩個異常雖然原因相似,但有本質區(qū)別2024-09-09Java字符串駝峰與下?lián)Q線格式轉換如何實現(xiàn)
這篇文章主要介紹了Java字符串駝峰與下?lián)Q線格式轉換如何實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11