请求参数

get 获取参数

@RequestPath(value = "/get")
public HttpResponse get(String before, String end, HttpRequest request) throws Exception {
  HttpResponse ret = Resps.html(request, "before:" + before + "<br>end:" + end);
  return ret;
}

这段代码定义了一个名为 get 的方法,通过 @RequestPath 注解映射到 URL 路径 /get。方法接收两个字符串参数 beforeend,以及一个 HttpRequest 对象 request

Post 获取参数

  @RequestPath(value = "/post")
  public HttpResponse post(String before, String end, User user, Short shortid, HttpRequest request) throws Exception {
    HttpResponse ret = Resps.html(request, "before:" + before + "<br>end:" + end + "<br>user:<pre>" + Json.toFormatedJson(user) + "</pre>");
    return ret;
  }

这段代码定义了一个名为 post 的方法,通过 @RequestPath 注解映射到 /post URL 路径。这个方法接收几个参数:两个字符串 beforeend,一个 User 对象 user,一个 Short 类型的 shortid,以及一个 HttpRequest 对象 request

这个方法的主要功能是接收 HTTP POST 请求,获取其中的参数和用户对象,并将这些信息以 HTML 格式的响应返回。

从请求地址中获取参数

示例代码

@RequestPath(value = "/var/{name}/{id}")
public HttpResponse var(String name, String id, HttpRequest request) throws Exception {
  HttpResponse ret = Resps.json(request, "name:" + name + "\r\n" + "id:" + id);
  return ret;
}

这段代码定义了一个处理 HTTP 请求的方法 var,该方法映射到一个 URL 路径模式 /var/{name}/{id}。这里的 {name}{id} 是路径变量,它们在 URL 中动态替换成实际的值。方法接收两个字符串参数 nameid,这些参数自动从匹配的 URL 中提取。还有一个 HttpRequest 参数,代表接收到的 HTTP 请求。

方法的主体创建并返回一个 HttpResponse 对象,其中包含 nameid 的值。通过 Resps.json 方法生成响应,它将 nameid 的值格式化为 JSON 格式的字符串。这样,当访问对应的 URL 时,此方法将以 JSON 格式返回提取的 nameid 参数值。