如何发出https请求?

时间:2019-03-15 11:24:19

标签: json spring https

我应该向地址请求并发送json,我以模型的形式发送

public class RequestToPlanfix {
    private String cmd;
    private String providerId;
    private String channel;
    private String chatId;
    private String planfix_token;
    private String message;
    private String title;
    private String contactId;
    private String contactName;
    private String contactLastName;
    private String contactIco;
    private String contactEmail;
    private String contactPhone;
    private String contactData;
    @Convert(converter = Attachments.class)
    private Map<String,String> name;
    @Convert(converter = Attachments.class)
    private Map<String,String> url;
    private Boolean isEcho;
}

也就是说,我应该通过https发送json,其字段与该实体的字段匹配,我如何获取该实体并将其转换为json?如何进一步提出https请求?

然后我将以json的形式收到答案,其中的字段是下一个实体的字段

也就是说,我将获取json并进行解析

public class RequestFromPlanfix {
    private String cmd;
    private String providerId;
    private String chatId;
    private String contactPhone;
    private String channel;
    private String token;
    private String message;
    private String userName;
    private String userLastName;
    private String userIco;
    private String taskEmail;
    @Convert(converter = Attachments.class)
    private Map<String,String> name;
    @Convert(converter = Attachments.class)
    private Map<String,String> url;
}

两种情况下的字段在某些地方是不同的,所以我创建了两个实体,然后当我们发送请求时,我们将发送第一个json,而当我们收到答案时,将发送第二个json,我不明白如何制作请求并将json与实体相关联,谢谢

enter image description here

   String postUrl = "www.site.com";
    Gson gson = new Gson();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(postUrl);
    StringEntity postingString = new StringEntity(gson.toJson(requestToPlanfix));//gson.tojson() converts your pojo to json
    post.setEntity(postingString);
    post.setHeader("Content-type", "application/json");
    HttpResponse response = httpClient.execute(post);

2 个答案:

答案 0 :(得分:1)

因此,您需要拨打HTTP POST

正如所解释的那样,您的request主体看起来像RequestToPlanfix.java,而您期望的Response则看起来像RequestFromPlanfix.java

此外,您正在使用GSON进行转换。

现在,我建议您将模型(RequestToPlanfix.javaRequestFromPlanfix.java重新定义为:

  • 转到:http://www.jsonschema2pojo.org/

  • 首先粘贴您的JSON请求,然后选择GSON, Java以及您认为有效的任何其他字段。

  • 然后从那里生成普通的Java旧对象。
  • 这样,我们可以确定JSON中没有错-JAVA了解。
  • 同样,根据您期望的response JSON,生成您的RequestFromPlanfix.java
  • 用上面生成的POJO替换现有的POJO。

现在,让我们使用Spring的HTTP call来制作RestTemplate

public class MakeRestCall {

  @Autowired
  private RestTemplate restTemplate;

  public void getResponse(){

      String postUrl = "www.site.com";

      // get your request object ready
      RequestToPlanfix requestObject = getRequestObject();

      // initialize HttpEntity 
      HttpEntity<RequestToPlanfix> httpEntity = new HttpEntity<>(requestObject);
      ResponseEntity<RequestFromPlanfix> response = restTemplate.exchange(postUrl, HttpMethod.POST, httpEntity, RequestFromPlanfix.class);
      RequestFromPlanfix responseObject = response.getBody();
      // carry-on with your other stuff
    }

希望这会有所帮助。

答案 1 :(得分:0)

您可以使用RestTemplate。

类似的东西:

@Test 
public void test() throws URISyntaxException {
    RestTemplate restTemplate = new RestTemplate();

    final String baseUrl = "https://your_url!";
    final URI uri = new URI(baseUrl);

    final RequestToPlanfix requestToPlanfix = new RequestToPlanfix();
    // set fields here to requestToPlanfix 

    final ResponseEntity<RequestFromPlanfix> result = restTemplate.postForEntity(uri, requestToPlanfix, String.class);

    // your response 
    final RequestFromPlanfix requestFromPlanfix = result.getBody();

    //Verify request succeed
    Assert.assertEquals(200, result.getStatusCodeValue()); 
 }

您可以找到其他示例here