package org.example.controller.requestparam; import org.apache.ibatis.jdbc.Null; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ResponseEntityController { // http://localhost:8080//responseEntity /** * 通过 ResponseEntity 处理响应 基础使用 * @param * @return */ @RequestMapping("/responseEntity") public ResponseEntity<String> greeting(){ return new ResponseEntity<>("hello there", HttpStatus.OK); } // http://localhost:8080//responseEntity-header /** * 通过 ResponseEntity 处理响应 添加自定义的http-header * @param * @return */ @RequestMapping("/responseEntity-header") public ResponseEntity<String> greetingHeader(){ HttpHeaders hds = new HttpHeaders(); hds.add("Custem-Header","custemHeader"); return new ResponseEntity<>("hello there", hds,HttpStatus.OK); } // httpStatus也可以返回不同的状态码 @RequestMapping("/responseEntity-null") public ResponseEntity<Long> greetingNull(){ return new ResponseEntity<>(null,HttpStatus.BAD_GATEWAY); } }
标签:http,responseEntity,spring,boot,return,ResponseEntity,org,import From: https://www.cnblogs.com/xiaobaibailongma/p/17068931.html