【问题标题】:Loading JSON data from a URL passing authentication parameters从传递身份验证参数的 URL 加载 JSON 数据
【发布时间】:2018-12-20 22:17:46
【问题描述】:

我必须使用来自 URL 的 JSON,我正在使用带有 jackson 的 Springboot,当我发布时,我在标头中发送了一些身份验证信息,API 需要 SECRETKEY + ACCESSKEY + date 我有所有这些信息要发送

 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方法的头部吗?

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

【问题讨论】:

  • 为了验证请求,您的 API 期望您做什么?如果您可以从 api 文档中发布有关如何进行身份验证的小说明,将有助于我们弄清楚
  • @slimane 很遗憾,我现在无法访问 API,但我知道她需要 SECRETKEY + ACCESSKEY + date 我有所有这些信息要发送..
  • 在 HEADER 标头中发送身份验证有点奇怪。 headers.add("HEADER", fullSignature);你可以试试授权吗
  • 但是 get 方法会是什么样子?
  • 在上面的代码中,你使用的是post方法。

标签: java spring-boot


【解决方案1】:

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

 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类来绑定你的响应json属性

public class MyResponseObject {

    @JsonProperty("relatorioID")
    private String relatorioID;



    @JsonProperty("professorID")
    private Integer professorID;

    ...


    //getter //setter
} 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2016-10-12
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多