spring-boot - 如何在 Spring Boot Rest Template 中删除和添加新的 Accept 标头?
问题描述
我正在使用GraphQL服务,如果Accept 标头为text/plain
.
它期待接受application/json
所以我尝试覆盖 RestTeamplate 标头。但是,Accept 标头似乎test/plain
总是存在在那里。我通过启用 debug( logging.level.org.springframework.web.client.RestTemplate=DEBUG
)确认了这一点
安慰
o.s.web.client.RestTemplate: Accept=[text/plain, application/json, application/*+json, */*]
o.s.web.client.RestTemplate: Writing [{products(query: "title:tow*", first: 10) {edges {node {id legacyResourceId title}}}}] as "application/graphql"
这是我试图覆盖 Accept 标头的代码
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
headers.add("Content-Type","application/graphql");
headers.setAccept(Collections.singletonList(new MediaType("application","json")));
String content = "{products(query: \"title:tow*\", first: 10) {edges {node {id title}}}}";
HttpEntity<String> requestEntity = new HttpEntity<String>(content, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
我什至尝试先删除 Accept 标头(headers.remove("Accept")
)然后setAccept
,但它仍然没有从日志中删除它。
还有什么需要做的吗?
为什么它不从 Accept 标头中删除 text/plain?
为什么我只设置一个选项时在接受中看到多个选项?
解决方案
推荐阅读
- linux - 在 CONTAINER 上设置 TCP keepalive
- if-statement - 十一:创建“查看收藏中的所有帖子”链接
- vb.net - 使用 Revit 在 Objectlistview 中创建复选框列
- javascript - 除输入焦点外,如何实现“按空格暂停html5视频”?(jQuery)
- php - 如何从 crm.lead.list 方法中获取下一组项目?
- javascript - 根据race_datetime对对象进行排序
- stripe-payments - 没有在 Stripe 仪表板中配置产品的情况下充电
- node.js - Stripe 不会为我的订阅创建发票
- freeswitch - 从 Freeswitch CHANNEL_HANGUP 事件确定哪个部分已断开呼叫
- python - Django mod_wsgi Apache 服务器,ModuleNotFoundError:没有名为 Django 的模块