猿问

我可以使用 Spring Boot 使用别人的 API 发出请求吗?

我对 Spring 很陌生,我正在尝试理解它。我可以使用Spring boot来调用别人的api吗?例如,我使用的 api 需要使用他们给我的凭据进行身份验证。我想使用他们的 API 来发出 POST 请求。到目前为止,我已经找到了与使用您自己的 API 相关的文档。这些请求还需要传递标头。

我可以使用 Spring 传递我的凭据和所需标头来执行 POST 请求的最简单方法是什么?

任何帮助,将不胜感激。


墨色风雨
浏览 186回答 3
3回答

慕哥6287543

如果我很了解你,你需要发布另一个带有凭据的网络API,你可以使用RestTemplate 就像下面的例子&nbsp;public List<EtisAccount> getAllActiveAccount(){&nbsp; &nbsp; &nbsp; &nbsp; logger.debug("Debug: in Class \t"+this.getClass().getName()+" Method Name is: \t"+new Object() {}.getClass().getEnclosingMethod().getName());&nbsp; &nbsp; &nbsp; &nbsp; Properties sprinklrProp=sprinklrProperties.getSprinklrKeys();&nbsp; &nbsp; &nbsp; &nbsp; SprinklrCredential sprinklrCredential=credentialBuilder.getSprinklrCredential();&nbsp; &nbsp; &nbsp; &nbsp; RestTemplate restTemplate= new RestTemplate();&nbsp; &nbsp; &nbsp; &nbsp; HttpHeaders header = new HttpHeaders();&nbsp; &nbsp; &nbsp; &nbsp; header.setBearerAuth(sprinklrCredential.getAccess_token());&nbsp; &nbsp; &nbsp; &nbsp; header.add("key", sprinklrCredential.getApi_key());&nbsp; &nbsp; &nbsp; &nbsp; header.set("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);&nbsp; &nbsp; &nbsp; &nbsp; UriComponentsBuilder uriBuilder= UriComponentsBuilder.fromUriString(sprinklrProp.getProperty("sprinlrUri").toString())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .queryParam("types", sprinklrProp.getProperty("accountTypes").toString());&nbsp; &nbsp; &nbsp; &nbsp; HttpEntity<String> entity= new HttpEntity<>(header);&nbsp; &nbsp; &nbsp; &nbsp; sslCertificateValidation.disable();&nbsp; &nbsp; &nbsp; &nbsp; ResponseEntity<String> sprinklrResponse=restTemplate.exchange(uriBuilder.toUriString(),HttpMethod.GET,entity,&nbsp; String.class);&nbsp; &nbsp; &nbsp; &nbsp; List<EtisAccount> activeAccouts=etisAccountHelper.getAllSocialEtisAccounts(sprinklrResponse.getBody());&nbsp; &nbsp; &nbsp; &nbsp; logger.debug(String.valueOf(sprinklrResponse.getStatusCodeValue()));&nbsp; &nbsp; &nbsp; &nbsp; logger.debug(activeAccouts.toString());&nbsp; &nbsp; &nbsp; &nbsp; return activeAccouts;&nbsp; &nbsp; }这是使用 RestTemplate 调用在线 API 的示例在这里我用凭据构建标头(不记名身份验证)&nbsp; &nbsp; &nbsp; &nbsp; HttpHeaders header = new HttpHeaders();&nbsp; &nbsp; &nbsp; &nbsp; header.setBearerAuth(sprinklrCredential.getAccess_token());&nbsp; &nbsp; &nbsp; &nbsp; header.add("key", sprinklrCredential.getApi_key());&nbsp; &nbsp; &nbsp; &nbsp; header.set("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);在这里我添加标头 requestEntity 以添加到 Resttemplate 请求&nbsp; &nbsp; &nbsp;HttpEntity<String> entity= new HttpEntity<>(header);在这里我使用交换方法得到响应&nbsp; &nbsp;ResponseEntity<String> sprinklrResponse=restTemplate.exchange(uriBuilder.toUriString(),HttpMethod.POST,entity,&nbsp; String.class);

大话西游666

你需要以下东西:从https://start.spring.io/创建一个 Spring Web 入门项目在项目中创建一个新的 Java 类并为其命名Controller。@RestController在类级别添加注释。在SpringBootApplication中配置RestTemplate对象在类中自动装配该RestTemplate对象Controller在类中创建一个方法Controller,使用RestTemplate,将调用@PostMapping他们的 API。

芜湖不芜

你可以尝试这个方法这是简单的方法您可以将用户名和密码设置为基本身份验证&nbsp; public class RESTInvoker {&nbsp; &nbsp; &nbsp; &nbsp; private final String baseUrl;&nbsp; &nbsp; &nbsp; &nbsp; private final String username;&nbsp; &nbsp; &nbsp; &nbsp; private final String password;&nbsp; &nbsp; &nbsp; &nbsp; public RESTInvoker(String baseUrl, String username, String password) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.baseUrl = baseUrl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.username = username;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.password = password;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String getDataFromServer(String path) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL url = new URL(baseUrl + path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URLConnection urlConnection = setUsernamePassword(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private URLConnection setUsernamePassword(URL url) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URLConnection urlConnection = url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String authString = username + ":" + password;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return urlConnection;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答