首页 > 其他分享 >请求放入对象列表中

请求放入对象列表中

时间:2024-10-25 13:48:47浏览次数:1  
标签:ApiMethod 请求 requestFormat url 列表 params import 放入 String

好的,你想要将解析到的GET请求包含的信息放到一个对象列表中,这样可以更方便地管理和使用这些信息。你可以定义一个包含URL、请求参数及其名称和请求格式的类,然后将解析到的信息存储到该类的对象列表中。

首先,定义一个类来存储GET请求的信息:

import java.util.Map;

public class ApiMethod {
    private String url;
    private Map<String, String> params;
    private String requestFormat;

    // Constructor
    public ApiMethod(String url, Map<String, String> params, String requestFormat) {
        this.url = url;
        this.params = params;
        this.requestFormat = requestFormat;
    }

    // Getters and Setters
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public String getRequestFormat() {
        return requestFormat;
    }

    public void setRequestFormat(String requestFormat) {
        this.requestFormat = requestFormat;
    }

    @Override
    public String toString() {
        return "ApiMethod{" +
                "url='" + url + '\'' +
                ", params=" + params +
                ", requestFormat='" + requestFormat + '\'' +
                '}';
    }
}

接下来,在主程序中使用这个类,并将解析得到的GET请求信息存储到对象列表中:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

public class ApiDocParser {

    public static void main(String[] args) {
        try {
            // URL to the doc-api.json file
            String apiDocUrl = "http://example.com/doc-api.json";

            // Fetch the content of the API doc
            String apiDocContent = fetchContentFromUrl(apiDocUrl);

            // Parse the JSON content
            JSONObject apiDocJson = new JSONObject(apiDocContent);

            // Assume the API methods are listed under a key called "methods"
            JSONArray methods = apiDocJson.getJSONArray("methods");

            // List to store ApiMethod objects
            List<ApiMethod> apiMethodList = new ArrayList<>();

            for (int i = 0; i < methods.length(); i++) {
                JSONObject method = methods.getJSONObject(i);

                // Only process GET methods
                if ("GET".equalsIgnoreCase(method.getString("method"))) {
                    String url = method.getString("url");
                    JSONObject paramsJson = method.getJSONObject("params");

                    // Convert params JSONObject to Map
                    Map<String, String> params = new HashMap<>();
                    Iterator<String> keys = paramsJson.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        String value = paramsJson.getString(key);
                        params.put(key, value);
                    }

                    // Assume request format is specified in the JSON
                    String requestFormat = method.getString("format");

                    // Create ApiMethod object and add to the list
                    ApiMethod apiMethod = new ApiMethod(url, params, requestFormat);
                    apiMethodList.add(apiMethod);
                }
            }

            // Output the list of ApiMethod objects
            for (ApiMethod apiMethod : apiMethodList) {
                System.out.println(apiMethod);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String fetchContentFromUrl(String urlString) throws Exception {
        StringBuilder result = new StringBuilder();
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
        }

        return result.toString();
    }
}

在这个示例中:

  1. ApiMethod 类包含了URL、参数和请求格式三个字段。
  2. 在主程序中,解析API文档的内容,并将所有GET请求的方法信息存储到ApiMethod对象列表中。
  3. 输出存储在列表中的ApiMethod对象。

你可以根据实际的API文档格式进一步调整代码,比如添加更多的字段或处理不同的请求格式。

标签:ApiMethod,请求,requestFormat,url,列表,params,import,放入,String
From: https://www.cnblogs.com/kakaBluce/p/18502355

相关文章

  • HttpClient请求结果
    好的,你想要循环遍历对象列表,对每个GET请求执行HTTP请求,并检查响应状态码是否为200。如果状态码不是200,就打印出响应体。为了实现这个功能,可以使用HttpClient库,它更现代和灵活。以下是一个示例代码,演示如何实现上述需求:首先,添加依赖(如果你使用Maven),在pom.xml中添加HttpClient库......
  • 为什么 Spring Boot 的微服务架构被称为“现代应用开发的曙光”?这种设计真的解决了传
    目录1.微服务架构为何被称为“现代应用开发的曙光”1.1单体架构的问题1.2微服务架构的诞生与发展1.3微服务架构的挑战2.SpringBoot在微服务中的角色2.1自动化配置与微服务开发2.2SpringCloud生态中的微服务3.微服务架构是否真的解决了传统单体架构中的所......
  • Vue axios发送请求
    Vue发送请求下载axios插件npminstallaxios-S具体操作:functionget(){//请求地址,参数,请求头;then是处理返回结果axios.get("http://localhost:8080/hello",{params:{},headers:{}}).then(res=>console.log(res));}fu......
  • 灰色代码部分:要是输入名字列表,又能输出结果,但是空列表的时候就输出不了?
    大家好,我是Python进阶者。一、前言前几天在Python白银交流群【Aciel】问了一个Python基础的问题,问题如下:灰色代码部分:要是输入名字列表,又能输出结果,但是空列表的时候就输出不了?二、实现过程这里【瑜亮老师】给了一个指导,具体如下所示:@Aciel 循环fornameinnames: 遍历......
  • 【ECMAScript】ajax请求
    【ECMAScript】ajax请求 普通表单,默认会带上cookie$.ajax({url:'/test/getUserInfo',method:'POST',data:{'token':token},success:function(response){console.log('Success:',response);},er......
  • dify的docker服务请求内网服务器遇到的问题
    接上一篇文章: https://www.cnblogs.com/neozheng/p/18400589 我有一台10.xxx.20.162的内网服务器运行着dify的dockercompose服务,又起了一台10.xxx.41.11的内网服务器用来运行ollama大模型。我是通过手动的方式安装的ollama,在运行ollama的时候遇到一个问题:ollam......
  • 【网络原理】——HTTP请求头中的属性
    阿华代码,不是逆风,就是我疯你们的点赞收藏是我前进最大的动力!!希望本文内容能够帮助到你!!目录一:HTTP请求头1:HOST2:Content-Length3:Content-Type(1)请求中常用的格式①application/json②application/x-www-form-urlencoded③multipart/form-data(2)响应中常用格式4:User......
  • Qt 进程保活(开源,国产环境)QTableWidget列表
    效果图第一步设计器拖拽一个QTableWidget和三个QPushButton,布局一下第二步上码1.mainwindow.h代码如下(示例):#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include<QDebug>#include<QPushButton>#include<QLabel>#include<QFileInfo......
  • 【神兵利器】——199、Burpsuite之请求重发模块
    基本介绍Burpsuite为渗透测试人员提供了请求重发的功能模块,渗透测试人员可以对捕获到的历史报文中的数据进行多次的更改进行Fuzzing尝试来对网站进行安全评估,同一个报文可以进行N次更改与重复测试评估,同时在最新的Burpsuite版本中提供了将多个请求组合放到一个请求包中进行发......
  • let 和 const 与 var 的区别;什么是同源策略?简述 HTTP 请求的过程
    同源策略是一种安全机制,它是浏览器对JavaScript实施的一种安全限制。所谓“同源”是指域名、协议、端口号均相同。同源策略限制了一个页面中的脚本只能与同源页面的脚本进行交互,而不能与不同源页面的脚本进行交互。这是为了防止恶意脚本窃取数据、进行XSS攻击等安全问题。......