使用javax的Reponse返回信息

1.绪言
  • javax.ws.rs.core.Response的Response类定义了很多符合HTTP规范的状态码,还以通过entity()方法向客户端返回任意类型的数据。
  • 基于spring boot,使用Response类代码示例如下:
@GetMapping("/testResponse")
public Response testResponse(@RequestParam(name = "name") String name) {
    Student exist = null;
    try {
        exist = studentService.findByName(name);
    } catch (Exception exception) {
        // 通过status()方法设置状态码,通过entity()设置返回的具体信息
        return Response.status(Response.Status.BAD_REQUEST).entity(exception.getMessage()).build();
    }
    return Response.ok().entity(exist).build();
}
  • 通过swagger查看response的具体内容
  1. 请求成功
{
  "statusType": "OK",
  "entity": {
    "id": 8,
    "name": "jack",
    "age": 22,
    "classId": 201306
  },
  "entityType": "example.lucy.com.demo.entity.Student",
  "metadata": {},
  "status": 200
}
  1. 请求失败
{
  "statusType": "BAD_REQUEST",
  "entity": "There is no student named cherry.",
  "entityType": "java.lang.String",
  "metadata": {},
  "status": 400
}
  • 总结: 在Spring boot项目中使用Response向客户端返回信息非常方便!
2. 使用Response类
  • 在maven中添加以下两个依赖即可
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- 不添加该依赖,运行会报错: ClassNotFound Exception : com.sun.ws.rs.ext.RuntimeDelegateImpl -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19</version>
</dependency>
  • 后来有一次,使用如下maven也ok了
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.32</version>
</dependency>
好的,您可以使用Java的Servlet API中的`javax.servlet.ServletOutputStream`类将文件流写入HTTP响应。以下是一个示例代码: ```java import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileDownloadServlet extends HttpServlet { private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = "/path/to/file"; // 文件路径 String fileName = "file_name.ext"; // 文件名 String mimeType = "application/octet-stream"; // MIME类型 // 设置响应头 response.setContentType(mimeType); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); // 获取文件输入流和输出流 FileInputStream fileInputStream = new FileInputStream(filePath); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, DEFAULT_BUFFER_SIZE); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); // 将文件流写入响应 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = bufferedInputStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } // 关闭输入流和输出流 bufferedInputStream.close(); fileInputStream.close(); bufferedOutputStream.flush(); bufferedOutputStream.close(); } } ``` 在上面的示例中,`doGet`方法响应GET请求并返回文件流。在该方法中,首先设置响应头,然后获取文件输入流和输出流。最后将文件流写入响应,并关闭输入流和输出流。需要注意的是,为了提高性能,使用了缓冲输入流和缓冲输出流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值