java - 在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() 之后)。提前致谢!
解决方案
推荐阅读
- javascript - 带有 DRM url 的 Google cast v3 CAF Receiver 应用程序
- html - 导航中的可点击框而不是框中的文本 - 显示块不起作用
- swift - 在 AudioKit 中停用 AudioSession
- c# - 从 json 字符串读取数据到 c# 对象
- google-apps-script - Gmail Addon 在启动授权之前保存表单输入
- docker - 由于套接字地址,Docker-compose 遇到错误
- azure - Service Fabric 分区标识符是否持久?
- react-native - 向 react-native-maps 添加国家视图
- angular - 将 nativescript 更新到 6.1 破坏了 Scss 编译
- ios - Swift 在关闭 Current ViewController 之后,Presenting ViewController 没有调用 viewWillAppear()