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

Java调用Akamai清除缓存返回200但未生效,求问题排查

问题排查与修复

核心问题分析

你的代码存在几个关键错误,导致接口返回200但缓存未被实际清除:

  1. 目标URL格式错误
    你定义的urlId有语法错误:https:://img.com包含两个冒号,应该修正为https://img.com。此外,Akamai CCU要求清除缓存的URL必须是完整的资源路径(比如https://img.com/logo.png),仅传入域名无法定位到具体缓存资源,会导致清除指令无效。

  2. 请求JSON格式非法
    拼接请求体时,没有给URL添加双引号,生成的JSON结构无效:

    {"objects":[https://img.com]}
    

    合法的JSON数组中字符串必须用双引号包裹,Akamai接口会忽略无效的请求体,仅返回成功状态码但不执行清除操作。

  3. URI构建语法错误
    创建URI时多了一个多余的+号:

    URI uri = new URI(HTTPS, HOST, "/ccu/v3/invalidate/url/production" + , null, null);
    

    这属于编译错误,需要删除多余的+才能正常运行。

  4. 状态码判断逻辑与实际行为不符
    Akamai CCU v3接口执行成功时返回201 Created,但你提到接口返回200,说明请求虽然被服务器接收,但因为请求体无效未执行清除操作。代码中仅在状态码为201时跳转成功页,返回200会进入失败分支,你可能没注意到这一点。

修正后的代码

import java.io.IOException;
import java.net.URI;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.akamai.edgegrid.signer.ClientCredential;
import com.akamai.edgegrid.signer.googlehttpclient.GoogleHttpClientEdgeGridRequestSigner;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;

public class MyServlet extends HttpServlet {

    private static final String HTTPS = "https://";
    private static final String HOST = "**********akamaiapis.net";

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int status = 0;
        ClientCredential credential = ClientCredential.builder()
                .accessToken("akab*********************")
                .clientToken("akab-*************************")
                .clientSecret("**************")
                .host(HOST).build();
        
        // 修正:使用完整的资源URL,确保格式正确
        String urlId = "https://img.com/path/to/your/resource.jpg";
        // 修正:正确拼接JSON,给URL添加双引号
        String json = String.format("{\"objects\":[\"%s\"]}", urlId);

        HttpTransport httpTransport = Utils.getDefaultTransport();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
        HttpRequest httprequest = null;
        try {
            // 修正:删除URI构建中的多余+号
            URI uri = new URI(HTTPS, HOST, "/ccu/v3/invalidate/url/production", null, null);
            httprequest = requestFactory.buildPostRequest(new GenericUrl(uri),
                    ByteArrayContent.fromString("application/json", json));
            
            GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(credential);
            requestSigner.sign(httprequest);
            
            HttpResponse httpresponse = httprequest.execute();
            status = httpresponse.getStatusCode();
            // 建议:打印响应体,排查接口返回的详细信息
            System.out.println(httpresponse.parseAsString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (status == 201) {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/PurgeSuccess.jsp");
            dispatcher.forward(request, response);
        } else {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ProductPurge.jsp");
            dispatcher.forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 可根据需求补充GET请求处理逻辑
    }
}

额外建议

  • 打印接口的响应体内容,Akamai会返回清除任务的详细信息(比如任务ID、预计生效时间),方便排查问题。
  • 确保你的Akamai凭证拥有ccu:invalidate的权限,权限不足也会导致指令无法执行。
  • Akamai缓存清除需要一定的生效时间(通常数分钟),执行成功后等待一段时间再验证缓存是否清除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 22:32:04