zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ResponseEntity类和HttpEntity及跨平台路径问题

2023-03-07 09:44:41 时间

1. 简介

使用spring时,达到同一目的通常有很多方法,对处理http响应也是一样。本文我们学习如何通过ResponseEntity设置http相应内容、状态以及头信息。

ResponseEntity是HttpEntity的扩展,添加一个HttpStatus状态代码。在RestTemplate和@Controller方法中使用。

ResponseEntity标识整个http相应:状态码、头部信息以及相应体内容。因此我们可以使用其对http响应实现完整配置

理解:

  1. ResponseEntity的优先级高于@ResponseBody。在不是ResponseEntity的情况下才去检查有没有@ResponseBody注解。如果响应类型是ResponseEntity可以不写@ResponseBody注解,写了也没有关系。
  2. ResponseEntity 是在 org.springframework.http.HttpEntity 的基础上添加了http status code(http状态码),用于RestTemplate以及@Controller的HandlerMethod。它在Controller中或者用于服务端响应时,作用是和@ResponseStatus与@ResponseBody结合起来的功能一样的。用于RestTemplate时,它是接收服务端返回的http status code 和 result的。
  3. 简单粗暴的讲 @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 指定响应状态码

静态方式

@GetMapping("/get")
public ResponseEntity<String> get() {
    return ResponseEntity.status(HttpStatus.LOCKED).body("服务不可用");
}

也可以通过非静态方式构建

@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");
        // 内容是字节流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 开始下载
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }

2.6 直接操作HttpServletResponse

Spring 也允许我们直接 javax.servlet.http.HttpServletResponse 对象;只需要申明其作为方法参数:

@GetMapping("/get")
public void get(HttpServletResponse response) throws IOException {
    response.setHeader("Custom-Header", "lisa");
    response.setStatus(200);
    response.getWriter().println("Hello World!");
}

但需要说明,既然spring已经提供底层实现的抽象和附件功能,当然不建议直接操作response。

3. 扩展(跨平台路径问题)

File.separator:系统相关的默认名称分隔符,为方便起见表示为字符串。该字符串只包含一个字符,即separatorChar

separatorChar:系统依赖的默认名称分隔符。这个字段被初始化为包含系统属性file.separator值的第一个字符。在UNIX系统上,这个字段的值是’\‘;在Microsoft Windows系统上它是’\\’

注意:如果要考虑跨平台,则最好使用File.separator标识路径分隔符,不要直接用字符串’\\'来表示