请求和响应字节

获取请求字节

package com.litongjava.tio.boot.hello.AController;

import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.server.annotation.RequestPath;
@AController
@RequestPath("/test/bytes")
public class TestBytesController {

  @RequestPath
  public String index(HttpRequest reuqest) {
    byte[] body = reuqest.getBody();
    return "index";
  }
}

Method index:

  • @RequestPath: 方法注解,没有提供具体的路径值,这意味着它将使用类级别的路径 /test/bytes。因此,当 HTTP 请求发送到 /test/bytes 时,此方法将被调用。
  • 方法接收一个 HttpRequest 对象作为参数,代表接收到的 HTTP 请求。
  • byte[] body = reuqest.getBody();: 从 HTTP 请求中获取字节数组格式的请求体。
  • 方法返回一个字符串 "index"。这个字符串会会作为 Response 返回。

这对深度学习框架很有用,可以它获取客户端发送的 NumPy 的字节数据

响应字节

直接响应

@RequestPath("/pen/raw/data")
@Slf4j
public class PenRawDataController {

  @RequestPath()
  public byte[] index() {
    byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    HttpResponse response = TioControllerContext.getResponse();
    response.setBody(bytes);
    return bytes;
  }
}

封装为HttpResponse响应

import com.litongjava.tio.http.common.HttpResponse;
@RequestPath("/pen/raw/data")
@Slf4j
public class PenRawDataController {

  @RequestPath()
  public HttpResponse index() {
    byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    HttpResponse response = TioControllerContext.getResponse();
    response.setBody(bytes);
    return response;
  }
}