当前位置: 首页 > 知识库问答 >
问题:

在Spring Framework resttemplate中将请求头内容类型设置为json[重复]

谢善
2023-03-14

我正在学习Spring框架,以创建使用基本身份验证和交换JSON的REST Web服务的客户端。在网上搜索了很多之后,我写了一些有效的代码(如下),但是现在我得到了一个“不支持的媒体类型”错误,因为请求是用内容类型文本/普通而不是应用程序/json发送的。我在网络上没有发现任何显示如何在请求头中设置内容类型的内容(而不会完全迷失在杂草中)。我的代码是:

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

...

BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("login", "password"));
HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

RestTemplate restTemplate = new RestTemplate(requestFactory);
String url = "http://host:8080/path/";
String postBody = getPostInput("filename");
jsonString = restTemplate.postForObject(path, postBody, String.class);

任何指导都将不胜感激。

谢谢,乔治

共有1个答案

樊俊悟
2023-03-14

您可以尝试使用下面代码中的任何方法

1.

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
restTemplate.put(uRL, entity);

2.

RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson); 
restTemplate.exchange(requestEntity, responseClass);

3个

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// if you need to pass form parameters in request with headers.
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", userName);
map.add("password", password);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<TokenVO> responses = restTemplate.postForEntity(URL, request, responseClass);
 类似资料:
  • 我试图使用服务/调度机制发布一个JAX-RS资源。问题是传出请求的内容类型被锁定为。我看不出有什么方法可以将其更改为其他类型,例如。 RESTful webservice只使用和。这是我使用的代码: 接受标头被修改为zoo标头,foo标头添加了值,但Content-Type保持不变。我想我可以使用一个过滤器,并根据一些条件修改,甚至基于foo头,但这似乎违反直觉。 以下是请求的所有标题: 感谢您的

  • 我正在使用npm“同构获取”发送请求。我遇到的问题是无法设置请求头的内容类型。 我将内容类型设置为application/json,但是请求头被设置为text/plain。 当我在浏览器中检查请求时,内容类型为o: 有人能解释为什么我不能设置这个属性吗?

  • 问题内容: 我正在开发一个React应用程序,并且正在使用fetch发送请求。我最近做了一个注册表单,现在将它与它的API集成在一起。以前,API接受url编码的数据,并且一切正常。但是现在需求已更改,API接受了JSON中的数据,因此我不得不将内容类型标头从“ application / x-www-form-urlencoded”更改为“ application / json”。但我收到以下错

  • 我试图向另一个接受内容类型“application/json-patch+json”的API发出补丁rest请求。我正在使用Spring的webclient,但我无法让它工作。我不断得到“415不支持的媒体类型” 我试过下面的; 我也试过: 对于这两种情况,我看到以下错误; 它似乎变成了'application/x-www-form-urlencoded;charset=utf-8',是否可能对此

  • 我无法在HttpClient上设置内容类型。我接着问了一个问题:如何为HttpClient请求设置Content-Type头?但还是没有运气。 所以我怎么能强迫它,这样它就真的添加了它呢?提前道谢。

  • null 当我创建正文部分时,我是否应该显式地设置top mime消息的内容类型,然后设置每个正文部分? 如果是,在上面的例子中它们应该是什么? 对于html建议,对于附件建议,对于内联建议。我正在使用所有这些,那么对于完整的消息和不同的主体部分应该是什么内容类型? 通过上面的代码,我得到了正确的html文本,纯文本,内联图像和附件在正确的地方与James集成的ThunderBird。 因此,我不