接受 JSON 和响应 JSON

手动处理 Json 请求

接收 Json 数据

从 http 请求中获取 json 字符串

从 http 请求中获取 json 字符串,接收 JSON 数据有需要手动转为 JavaBean

String bodyString = request.getBodyString();

示例代码

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

import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath(value = "/getBodyString")
  public HttpResponse getBodyString(HttpRequest request) throws Exception {
    String bodyString = request.getBodyString();
    log.info(bodyString);
    HttpResponse ret = Resps.txt(request, bodyString);
    return ret;
  }

}
  1. @RequestPath: @RequestPath("/test/json"),用于声明这个类是一个请求控制器,并指定了访问这个控制器的基础 URL 路径。在这种情况下,任何发送到 /test/json 的 HTTP 请求都将由这个控制器处理。

  2. Method getBodyString:

    • @RequestPath(value = "/getBodyString"): 这个方法注解指定了具体的请求路径。当 HTTP 请求发送到 /test/json/getBodyString 时,将调用此方法。
    • 方法接收一个 HttpRequest 对象作为参数,代表接收到的 HTTP 请求。
    • request.getBodyString(): 从 HTTP 请求体中获取字符串内容。
    • log.info(bodyString): 使用日志记录请求体的内容。
    • HttpResponse ret = Resps.txt(request, bodyString): 创建一个 HttpResponse 对象来响应请求。Resps.txt 方法创建了一个文本响应,内容为请求体中的字符串。
    • return ret;: 返回构建的响应对象。 请求信息
curl --location --request POST 'http://127.0.0.1/test/json/getBodyString' \
--header 'User-Agent: apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1' \
--header 'Connection: keep-alive' \
--data-raw '{
    "ip": "49.174.104.160",
    "loginName": "引需必装起",
    "nick": "邹霞"
}'

通过实体类接受 JSON 和响应 JSON

实体类

package top.ppnt.java.ee.tio.http.server.boot.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class User {
  private Integer id;
  private String loginName;
  private String nick;
  private String ip;
}

自动封装为 java bean

在 Action 的方法签名上添加参数 User user

示例代码
import com.litongjava.tio.boot.hello.model.User;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath(value = "/bean")
  public User bean(User user, HttpRequest request) throws Exception {
    return user;
  }

}

自动封装为 java bean 支持下面这些格式的请求

支持 POST 请求格式 application/json
POST /demo/bean HTTP/1.1
Host: 127.0.0.1
Content-Type: application/json

{
  "ip": "69.134.20.34",
  "loginName": "别同器",
  "nick": "汪刚"
}
支持 POST 请求格式 application/x-www-form-urlencoded

GET /demo/bean HTTP/1.1
Host: 127.0.0.1
Content-Type: application/x-www-form-urlencoded

loginName=Ping%20E%20Lee&nick=%E6%9D%8E%E9%80%9A&ip=127.0.0..1

支持 POST 请求格式 multipart/form-data

示例

POST /demo/bean HTTP/1.1
Host: 127.0.0.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW

支持 Get 请求 Url 传递参数

示例如下


GET /demo/bean?loginName=Ping%20E%20Lee&nick=%E6%9D%8E%E9%80%9A&ip=127.0.0..1 HTTP/1.1
Host: 127.0.0.1

返回 Json 数据

直接返回 json 数据 Action 的返回值可以直接是实体类,框架会自动进行转换

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

import com.litongjava.tio.boot.hello.model.User;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath(value = "/responseUser")
  public User responseUser(HttpRequest request) throws Exception {
    return User.builder().loginName("Ping E Lee").nick("李通").ip("127.0.0.1").build();
  }
}

@RequestPath(value = "/responseUser"): 方法注解,指定了此方法处理的具体请求路径。当 HTTP 请求发送到 /test/json/responseUser 时,此方法将被调用。 方法返回 User 类型的对象。这里使用了 User 类的 builder 模式创建了一个 User 对象,并设置了相关属性。 该方法无需额外的 HTTP 响应处理,因为 tio-boot 框架将自动处理 User 对象的序列化并返回 JSON 格式的响应。 响应

{
  "ip": "127.0.0.1",
  "loginName": "Ping E Lee",
  "nick": "李通"
}

使用 Resps.json(request, user);返回 json 数据


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

import com.litongjava.tio.boot.hello.model.User;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath(value = "/responseJson")
  public HttpResponse json(HttpRequest request) throws Exception {
    User user = User.builder().loginName("Ping E Lee").nick("李通").ip("127.0.0.1").build();
    HttpResponse ret = Resps.json(request, user);
    return ret;
  }
}

Method responseJson:

  • @RequestPath(value = "/responseJson"): 方法注解,指定了此方法处理的具体请求路径。当 HTTP 请求发送到 /test/json/responseJson 时,此方法将被调用。
  • 方法内部创建了一个 User 对象,使用了 Builder 模式设置了其属性。
  • 使用 Resps.json(request, user) 生成了一个包含 JSON 格式用户数据的 HttpResponse 对象。这种方式是 tio-boot 框架中处理和返回 JSON 数据的标准做法。
  • 最后,方法返回这个 HttpResponse 对象。

请求 http://127.0.0.1/demo/responseJson 响应

{
  "ip": "127.0.0.1",
  "loginName": "Ping E Lee",
  "nick": "李通"
}

使用 Resp 返回 Json 数据 核心代码


return Resps.json(request, Resp.ok(systemInfo));


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

import com.litongjava.tio.boot.hello.model.User;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;
import com.litongjava.tio.utils.resp.Resp;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath("/responseJsonResp")
  public HttpResponse responseJsonResp(HttpRequest request) {
    User user = User.builder().loginName("Ping E Lee").nick("李通").ip("127.0.0.1").build();
    return Resps.json(request, Resp.ok(user));
  }
}

Method responseJsonResp:

  • @RequestPath("/responseJsonResp"): 方法注解,指定了此方法处理的具体请求路径。当 HTTP 请求发送到 /test/json/responseJsonResp 时,此方法将被调用。
  • 方法内部创建了一个 User 对象,使用了 Builder 模式设置了其属性。
  • 使用 Resps.json(request, Resp.ok(user)) 生成了一个包含 JSON 格式用户数据的 HttpResponse 对象。Resp.ok(user) 封装了 User 对象,并添加了一些额外的响应信息,例如成功状态。
  • 最后,方法返回这个 HttpResponse 对象。 返回数据如下
{
  "data": "数据部分",
  "ok": true
}
使用 RespVo 返回 Json 数据

核心代码

return Resps.json(request, RespVo.ok(data));

示例

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

import com.litongjava.tio.boot.hello.model.User;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.annotation.RequestPath;
import com.litongjava.tio.http.server.util.Resps;
import com.litongjava.tio.utils.resp.Resp;
import com.litongjava.tio.utils.resp.RespVo;

import lombok.extern.slf4j.Slf4j;
@AController
@RequestPath("/test/json")
@Slf4j
public class TestJsonController {

  @RequestPath("/responseJsonRespVo")
  public HttpResponse responseJsonResps(HttpRequest request) {
    User user = User.builder().loginName("Ping E Lee").nick("李通").ip("127.0.0.1").build();
    return Resps.json(request, RespVo.ok(user));
  }
}

Method responseJsonResps:

  • @RequestPath("/responseJsonRespVo"): 方法注解,指定了此方法处理的具体请求路径。当 HTTP 请求发送到 /test/json/responseJsonRespVo 时,此方法将被调用。
  • 方法内部创建了一个 User 对象,使用了 Builder 模式设置了其属性。
  • 使用 Resps.json(request, RespVo.ok(user)) 生成了一个包含 JSON 格式用户数据的 HttpResponse 对象。RespVo.ok(user) 封装了 User 对象,并可能添加了一些额外的响应信息,例如成功状态,这是一种标准的响应对象格式。
  • 最后,方法返回这个 HttpResponse 对象。 返回数据如下
{
  "data": "数据部分",
  "ok": true
}