gpt4 book ai didi

java - 从传递身份验证参数的 URL 加载 JSON 数据

转载 作者:行者123 更新时间:2023-12-02 10:27:03 26 4
gpt4 key购买 nike

我必须从 URL 中使用 JSON,我正在使用 Springboot 和 jackson,当我发帖时,我在 header 中发送一些身份验证信息,API 需要 SECRETKEY + ACCESSKEY + 日期,我有所有这些信息要发送

 public void sendListPayload(int count, List object, String controller) throws NoSuchAlgorithmException, IOException {

Control type = Control.valueOf(controller);
String endereco = getAdress(type);
String payloadSecure = "";
RestTemplate restTemplate = new RestTemplate();
String url = "http://adress/site.php";
HttpHeaders headers;
String payload = convertListToJson(object);
headers = getHeaders(count, payload);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(payloadSecure, headers);
String answer = restTemplate.postForObject(url, entity, String.class);
log.info(answer);
}

获取标题

public HttpHeaders getHeaders(int sizeRecords, String payloadSecure) throws NoSuchAlgorithmException {
HttpHeaders headers = new HttpHeaders();
String signature = "";
signature = payloadSecure + SECRETKEY + ACCESSKEY + getISODate();
String fullSignature = FIRSTPAYLOAD + getISODate() + ":" + Useful.toSha(signature);
headers.add("HEADER", fullSignature);
return headers;

我要读取的缩小 JSON 将如下所示

[{"relatorioID":"1852","professorID":"7","alunoID":"37","turmaID":"44","bimestre":"0","data":"2014-06-05 07:51:49","situacao":"1"},
{"relatorioID":"1854","professorID":"7","alunoID":"37","turmaID":"44","bimestre":"0","data":"2014-06-05 07:51:55","situacao":"1"}]

我已经有一个具有相同字段的对象来使用 JSON 数据实例化我对java和springboot很陌生,如何通过secretkey和accesskey?是在get方法的头部吗?

然后我必须使用 jackson ... 将接收到的 JSON 转换为对象列表,以将它们插入到本地数据库中。

最佳答案

因此,重点是您需要清楚 API 规范,即您的 API 期望以哪种格式请求请求。无论如何,回答下面的问题代码可以帮助您在 header 中发送身份验证参数并处理响应。

 import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.http.ResponseEntity;
//some more class import may be you need to add
try {

UriComponents uriComponents =
UriComponentsBuilder.newInstance().scheme("https").host(host).path(url).
queryParam("url_param1", value).queryParam("another_param",
value).build().encode();

HttpHeaders headers = new HttpHeaders();
headers.add("SECRETKEY", value);
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
ResponseEntity<List<MyResponseObject>> response = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, entity, new ParameterizedTypeReference<List<MyResponseObject>>());

List<MyResponseObject > responses= response.getBody();

} catch (Exception e) {
logger.error(e.getMessage());
}

创建 MyResponseObject 类来绑定(bind)您的响应 json 属性

public class MyResponseObject {

@JsonProperty("relatorioID")
private String relatorioID;



@JsonProperty("professorID")
private Integer professorID;

...


//getter //setter
}

我希望这将帮助您将参数绑定(bind)在 header 上,并通过在 URL 中编码查询参数来发送。一旦您获得 MyResponseObject 对象列表形式的响应。

关于java - 从传递身份验证参数的 URL 加载 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com