You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java调用x-ui接口删除入站客户端失败,Postman可正常执行

问题定位与解决方案

核心问题分析

你的代码存在几个关键错误,导致请求无法被x-ui正确处理:

  1. 构造了请求体但未实际使用
    你拼接了requestBody变量,但创建HttpEntity时传入的是"{}"空JSON,这会让x-ui接口无法获取到删除客户端所需的参数,而Postman中你肯定传递了正确的请求体,这是最主要的原因。

  2. 接口URL路径错误
    x-ui的delClient标准接口路径并非/panel/api/inbounds/{protocol}/delClient/{clientId},正确的路径通常是/panel/api/inbounds/delClient,需要通过请求体传递inbound的ID和客户端ID,而非拼接在URL中。

  3. 手动拼接JSON的转义错误
    你手动拼接嵌套JSON时的转义逻辑容易出错,比如settings字段的嵌套JSON被额外转义,可能导致接口解析失败。

修正后的代码实现

以下是修复后的完整代码,解决了上述所有问题:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public void removeClient(String inboundId, String clientId) {
    if (clientId == null || inboundId == null) {
        System.out.println("Client ID or Inbound ID is null, nothing to remove.");
        return;
    }

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Cookie", loginCookie);
    headers.add("Accept", "application/json");

    // 使用Map构造参数,避免手动JSON转义错误
    Map<String, Object> requestBody = new HashMap<>();
    requestBody.put("id", inboundId); // 这里传入的是inbound的ID,不是固定值1
    requestBody.put("clientId", clientId);

    ObjectMapper objectMapper = new ObjectMapper();
    String requestBodyJson;
    try {
        requestBodyJson = objectMapper.writeValueAsString(requestBody);
    } catch (Exception e) {
        System.out.println("Failed to generate request body JSON: " + e.getMessage());
        return;
    }

    HttpEntity<String> request = new HttpEntity<>(requestBodyJson, headers);
    // 使用x-ui标准的delClient接口路径
    String removeUrl = xuiBaseUrl + "/panel/api/inbounds/delClient";

    try {
        ResponseEntity<String> response = restTemplate.postForEntity(removeUrl, request, String.class);
        System.out.println("Remove client status: " + response.getStatusCode());
        System.out.println("Remove client response: " + response.getBody());

        if (response.getStatusCode() == HttpStatus.OK) {
            if (response.getBody() != null && response.getBody().contains("success")) {
                System.out.println("Client removed successfully: " + clientId);
            } else {
                System.out.println("Client removal response does not indicate success, check x-ui logs.");
            }
        } else {
            throw new RuntimeException("Failed to remove client: HTTP status " + response.getStatusCode());
        }
    } catch (Exception e) {
        System.out.println("Error removing client: " + e.getMessage());
    }
}

额外验证步骤

  1. 确认接口路径正确性
    登录x-ui后台,打开浏览器开发者工具,在删除客户端时观察Network请求的URL,确保代码中的removeUrl与实际请求路径一致。

  2. 验证Cookie有效性
    将Postman中使用的Cookie完整复制到loginCookie变量中,确保包含所有必要的会话字段(如session_id等),避免Cookie过期或不完整。

  3. 检查inbound ID
    代码中的inboundId需要传递对应协议的inbound的ID(不是协议名称),你可以在x-ui后台的入站列表中查看每个inbound的ID值。

内容的提问来源于stack exchange,提问作者user24823924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 18:15:12