GET请求可在Postman中工作,但不能与RestTemplate-SpringBoot

时间:2019-07-31 07:04:43

标签: spring spring-boot resttemplate

当我尝试通过邮递员发送邮件时,我的请求运行正常。 我正尝试使用遇到错误的代码来实现同样的目的。

我正在使用的代码-

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

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("client_id", clientid);
map.add("client_secret",clientsecret);

HttpEntity<?> request = new HttpEntity<>(map, headers);
logger.info(request.toString());

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(access_token_url)
                    .queryParam("grant_type", "client_credentials");

logger.info("URI -" + builder.toUriString());

ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), 
   HttpMethod.GET, request, String.class);
logger.info("Response" + response.getBody());

我得到的错误是-

  

org.springframework.web.client.HttpClientErrorException $未经授权:   401未经授权

但是在POSTMAN中具有相同细节的功能相同。

我要指定的内容

  • 后端API不在我手中。
  • 从属性文件中获取了clientid,clientsecret,access_token_url,并且正确地对它们进行了交叉检查。
  • 这是一个带有x-www-form-urlencoded数据的GET请求。我多次检查它不是POST
  • 由于没有在POSTMAN中设置,因此没有诸如基本身份验证之类的授权
  • 通过This ThisThis都没有帮助。

我主要担心的是,我从未见过用方法体发送GET请求的任何地方,但是它在POSTMAN中工作,如果在那里工作,为什么不在代码中?我要去哪里错了?

编辑-1 用This标记为重复,但总体上来说是不同的错误代码。 OP给出了错误的内容类型,得到了500 Internal Server Error

编辑-2 调试输出-

2019-07-31 14:53:05.208 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate              : HTTP GET ORIGINAL_URL?grant_type=client_credentials
2019-07-31 14:53:05.215 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate              : Accept=[text/plain, application/json, application/*+json, */*]
2019-07-31 14:53:05.218 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate              : Writing [{client_id=[CLIENTID], client_secret=[CLIENT_SECRET]}] as "application/x-www-form-urlencoded"
2019-07-31 14:53:06.976 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate              : Response 401 UNAUTHORIZED
2019-07-31 14:53:07.034 DEBUG 3576 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-07-31 14:53:07.035 DEBUG 3576 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Nothing to write: null body
2019-07-31 14:53:07.039 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2019-07-31 14:53:07.108 DEBUG 3576 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : GET "/favicon.ico", parameters={}
2019-07-31 14:53:07.119 DEBUG 3576 --- [io-8080-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []]
2019-07-31 14:53:07.181 DEBUG 3576 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

2 个答案:

答案 0 :(得分:1)

对于相同的用例,我们使用以下实现。

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
String token = new String(Base64.getEncoder().encode((clientId + ":" + clientSecret).getBytes()));
headers.add("Authorization", "Basic " + token);
HttpEntity<?> request = new HttpEntity<>(headers);
logger.info(request.toString());
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(access_token_url).queryParam("grant_type", "client_credentials");
logger.info("URI -" + builder.toUriString());
ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,request, String.class);
logger.info("Response" + response.getBody());

这可能会对您有所帮助。

答案 1 :(得分:0)

我怀疑以下情况。您正在对网址进行双重编码,请更改:

restTemplate.exchange(builder.build().encode().toUri(), 
    HttpMethod.GET, request, String.class);

restTemplate.exchange(builder.build().toUri(), 
    HttpMethod.GET, request, String.class);

您可以通过启用Spring Web调试轻松匹配您的请求。

application.properties

logging.level.org.springframework.web: DEBUG