首页 > 解决方案 > 在GET响应Header中生成ETag Java spring解决412 http错误

问题描述

我正在使用一些外部 API 来获取和发布一些资源,在本地,我的代码在调用不同的端点(GET、POST ...)甚至使用 Postman 时都可以正常工作,但是当我尝试在另一个平台中运行我的代码时(其中资源是),由于 POST 调用,我收到 412 HTTP 错误:查看互联网后,我发现我应该生成实体的 ETagd(我去修改)并将其添加到我的标题中POST 端点。为此,我使用了ShallowEtagHeaderFilter和 @Bean 注释(在过滤方法上方)和我的类上方的 @SpringBootApplication 注释,这是我的代码:

package main.Runners;

import io.testproject.java.annotations.v2.Parameter;
import okhttp3.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.Filter;


@SpringBootApplication
public class ActionRunner {

    @Parameter(description = "the project ID")
    public static String projectId = "xxx";

    @Parameter(description = "the test ID")
    public static String testId = "yyy";


    public static void main(String[] args) throws Exception {

        try {

            OkHttpClient client = new OkHttpClient().newBuilder()
                                          .build();
            Request request = new Request.Builder()
                                      .url("https://api.testproject.io/v2/projects/"+projectId+"/tests/"+testId)
                                      .method("GET", null)
                                      .addHeader("Authorization", "nzmo4DI08ykizYgcp9-5cCTArlxq7k7zt9MYhGmTcRk1")
                                      .build();
            Response response = client.newCall(request).execute();

            System.out.println("================ this is our response headers ::: \n"+ response.headers());

        } catch(Exception e) {
            System.out.println(e);
        }
    }

    @Bean
    public ShallowEtagHeaderFilter shallowEtagHeaderFilter(){
        return new ShallowEtagHeaderFilter();
    }
}

我真的需要您的帮助,因为我无法在我的 GET 响应标头上生成任何 ETag 参数(在检查 reponse.headers() 之后)。提前致谢!

标签: javaspring-bootapietaghttp-status-code-412

解决方案


推荐阅读