1. Explanation
-
ResponseEntity
Used to identify the entire Http response, it can identify the status code, header information, and response body. -
ResponseEntity
PriorityAbove@ResponseBody。
Only check if the return value is not a ResponseEntity.
If the response type is ResponseEntity, you can not write the @ResponseBody annotation, and you can return JSON data or other types of data. If you use both ResponseEntity and @ResponseBody annotation, you will not report an error.
- ResponseEntity is in
Added on the basis of
http status code
(http status code). The function is and@ResponseStatus
and@ResponseBody
The combination of functions is the same. - @ResponseBody can directly return JSON results. @ResponseEntity can not only return JSON results, but also return custom HttpHeaders and HttpStatus.
2. ().headers (response header).body (response body)
Can be used for file download
and directHttpServletResponse
Write OutputStream and header in the same way
HttpServletResponse is a servlet-style writing method, while ResponseEntity is a Spring-style writing method
-
new ResponseEntity<>(response body, response header, status code)
Abbreviation of right and wrong -
().headers(response header).body(response body)
It's abbreviation
import ; import ; import ; import ; import ; import ; import ; import ; import ; @GetMapping("/testResponseEntity1") public ResponseEntity<byte[]> testResponseEntity1() throws Exception { // Read local files String filePath = "/temp/A110120119/test file.text"; ClassPathResource readFile = new ClassPathResource(filePath); // Set the response header, put the file name into the response header, and make sure the file is downloadable HttpHeaders headers = new HttpHeaders(); ("Content-Disposition", "attachment;filename=" + ((), "UTF-8")); // Set the content type to "application/octet-stream" binary stream (MediaType.APPLICATION_OCTET_STREAM); // Get File object File file = (); Path path = (()); // Get the bytecode file of the File object byte[] bytes = (path); /* * Indicates that a response that returns a bytecode type * The response header and status code are set at the same time * */ if ((())) { // 👉👉👉👉Abbreviation return ().headers(headers).body(bytes); } // 👉👉👉👉Non-abbreviated form return new ResponseEntity<>(bytes, headers, ); }
3. (Response content)
The request is successful, and the data in the background is directly responded to the front desk.
@GetMapping("/testResponseEntity2") public ResponseEntity<List<String>> testResponseEntity2() { List<String> list = ("1", "2"); return (list); }
The same function as the following writing method
@GetMapping("/testResponseEntity2") @ResponseBody public List<String> testResponseEntity2() { List<String> list = ("1", "2"); return list; }
IV. ResponseEntity<Void>
-
HttpStatus.NO_CONTENT
The status code is 204, indicating that the server successfully processed the request, but did not return any content.
Mostly used forrenewanddeleteWhen the response is to the front desk, a status code is given to the front desk, indicating that the operation is successful.
import ; import ; @GetMapping("/testResponseEntity3") public ResponseEntity<Void> testResponseEntity3(String param) { // ⏹http status code 204 (no content) The server successfully processed the request, but did not return any content. if ((param)) { // ⏹Abbreviation return ().build(); } // ⏹Not abbreviation return new ResponseEntity<>(HttpStatus.NO_CONTENT); }
5. (Status Code)
Used to return the specified status code to the foreground, and can also return the header and response content.
-
of201The status code indicates that the request was successful and the server created a new resource.
@GetMapping("/testResponseEntity4") public ResponseEntity<Void> testResponseEntity4() { // Insert data into the database // ...... // ⏹http status code 201 (created) The request was successful and the server created a new resource. return ().build(); } @GetMapping("/testResponseEntity5") public ResponseEntity<String> testResponseEntity5(String param) { // If the parameter does not exist, return the default image url and return the status code 201 if (!(param)) { return new ResponseEntity<>("Default image URL", ); } // Insert a picture into the database and return the URL that can access the image address // ...... // ⏹ Used to return data to the front desk after inserting data successfully // ⏹201 status code and return the url of the picture return ().body("The url of the picture"); }
6. (Status Code).body (Response Body)
-
HttpStatus.BAD_REQUEST
Indicates the status code 400, an exception request.
@GetMapping("/testResponseEntity6") public ResponseEntity<Map<String, Object>> testResponseEntity6(String param) { // Map used to store verification information Map<String, Object> map = new HashMap<>(); // Perform parameter verification if (param == null) { // The parameter is null, and the error code 400 is returned directly return (HttpStatus.BAD_REQUEST).build(); } else if ("".equals(())){ // The parameter is an empty string, and the error code 400 is returned, and the error message is also returned. ("errorMsg","Parameter is empty"); return ().body(map); } // 200 status code and specify the response body for the successful request ("successMsg", "Parameters pass verification"); return ().body(map); // This writing method is simpler, essentially the same as above. // return (map); }
7. Front desk ajax
$.ajax({ url: `askURL`, type: 'Request method', // data: (param), // The type of data sent to the server // contentType: 'application/json;charset=utf-8', // dataType: 'json', success: function (data, status, xhr) { // Response body that successfully requested (data); // The status text description of the successful request (success, nocontent, etc.) (status); // The status code for successful request (200, 201, 204, etc.) const { status: stateCode } = xhr; (stateCode); }, error(xhr, status, error) { const { // json format response body when requesting exception responseJSON, // Text response content when requesting exception responseText, // Status code when requesting an exception status: stateCode } = xhr; (responseJSON); (responseText); (stateCode); // The status text description of the successful request (error, etc.) (status); } });
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.