RestTemplate the GET and POST

  • Send GET request:
  • // set the request header
    HttpHeaders headers = new new HttpHeaders (); headers.add ( "token" , PostUtils.getToken ()); // call the required interfaces token, if the project did not add permissions may not need this token interception set
    / / request body the HttpEntity
    <String> = RequestEntity new new the HttpEntity <> ( null , headers);
    // initiation request RestTemplate RestTemplate
    = new new RestTemplate (); ResponseEntity <the JSONObject> jsonObjectResponseEntity = restTemplate.exchange ( " interface address call ", HttpMethod. . GET, RequestEntity, JSONObject class );
    // get the data into the format you want responseLockList responseLockList
    =JSON.parseObject(jsonObjectResponseEntity.getBody().toString(), new TypeReference<ResponseLockList>() {}); 
  • Send POST request:
  • // parameters into
    the JSONObject jsonObj = null;
    // setup request header
    HttpHeaders headers = new new HttpHeaders (); headers.setContentType (MediaType.APPLICATION_JSON_UTF8); the reference mode request is // JSON headers.add ( "token" , getToken to ( )); // call interface required token, if the project does not have permission to intercept the token may not need to set
    // the request body the HttpEntity
    <String> = formEntity new new the HttpEntity <String> (jsonObj.toString (), headers);
    // transmission request RestTemplate RestTemplate
    = new new RestTemplate ();
    // return json string string json
    = restTemplate.postForObject ( " interface address called .", formEntity, string class );
    // convert the string to an object json ResponseSaasDel pwd
    = JSON.parseObject (json, new new typereference <ResponseSaasDel> () {});

     

  • Reference: https://blog.csdn.net/likekobe2012/article/details/82663725

Guess you like

Origin www.cnblogs.com/LJing21/p/11935001.html
Recommended