如何对接受参数的方法进行 REST API 调用
Posted
技术标签:
【中文标题】如何对接受参数的方法进行 REST API 调用【英文标题】:how to make a REST API call to a method that accepts parameters 【发布时间】:2020-05-01 20:24:58 【问题描述】:我对 api 调用很陌生。我得到了一个生成令牌的 java 类。我被要求构建一个对 SecurityUtil 进行 api 调用的 Web 服务 .calculateAuthorizationSignature(fields, clientId, clientSecret) 方法使用那些
参数而不是硬编码,如下面的类所示:
公共类 SecurityUtil public static void main(String[] args)
String[] fields = new String[3];
// POC+100+05QQAWQERQWHYTFDYUSwY
fields[0] = "POC";
fields[1] = "100";
fields[2] = "05QQAWQERQWHYTFDYUSwY2";
String clientId = "dfaaa525-704c-41f4-9d95-7983f9bee18d";
String clientSecret = "6r9186uxrt031lw0diivck9noma1onfq";
String signatureStr = new SecurityUtil()
.calculateAuthorizationSignature(fields, clientId, clientSecret);
System.out.println(signatureStr);
public String encodeBase64(String val)
return Base64.getEncoder().encodeToString(val.getBytes());
public String decodeBase64(String val) throws UnsupportedEncodingException
return new String(Base64.getDecoder().decode(val), "ASCII");
public String hmacSha256(String val, String key)
return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(val);
public String calculateAuthorizationSignature(String[] fields, String id, String secret)
StringBuilder sb = new StringBuilder();
boolean addSeparator = false;
for (String s : fields)
if (addSeparator)
sb.append("+");
sb.append(s);
addSeparator = true;
String serverSignature = hmacSha256(sb.toString(), secret);
String clientId = encodeBase64(id);
Instant instant = Instant.now();
Long timeStampMillis = instant.getEpochSecond();
String timeStamp = encodeBase64(String.valueOf(timeStampMillis));
String cipher = serverSignature + "." + timeStamp + "." + clientId;
return encodeBase64(cipher);
我正在使用spring boot,我的pom文件如下所示:
https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.发布 com.abelinho.securityutil 安全演示 0.0.1-快照 安全演示 Spring Boot 演示项目
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我的项目结构如下图:
1
请提供帮助。谢谢大家!
【问题讨论】:
【参考方案1】:要进行 API 调用,您需要使用任何 http 客户端,例如 RestTemplate 或 FeignClient 使用您可以调用 API 的 rest 模板,
fields[0] = "POC";
fields[1] = "100";
fields[2] = "05QQAWQERQWHYTFDYUSwY2";
String clientId = "dfaaa525-704c-41f4-9d95-7983f9bee18d";
String clientSecret = "6r9186uxrt031lw0diivck9noma1onfq";
public String copyAssests(String clientId , String clientSecret, String[] fields)
return restTemplate.exchange("url", HttpMethod.POST, getHttpEntity(request, null, appCode), String.class, fields).getBody();
private <T> HttpEntity<T> getHttpEntity(T t, String authorization, String appCode)
HttpHeaders headers = new HttpHeaders();
headers.add("header", "value");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(t, headers);
【讨论】:
以上是关于如何对接受参数的方法进行 REST API 调用的主要内容,如果未能解决你的问题,请参考以下文章
Bitbucket REST Api:如何通过调用 rest api 按日期 desc 对所有分支进行排序?
Spring boot REST API 仅接受来自 Angular 前端的调用
如何在Java Jersey REST服务中强制使用queryparams?