To trigger a simple get request we can use following code
@GetMapping("triggersimpleget")
public void triggersimpleget() {
try {
/**
* request will be sent to url
*/
String url = "http://localhost:1818/simpleget";
HashMap<String, String> queryparams = new HashMap<String, String>();
/**
* setting header to accept json
*/
RestTemplate restTemplate = new RestTemplate();//instead of this we can create a bean and autowire it.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
/**
* exchange( urltobehit, httpmethod, header, response, urivaraibles
* we are sending get request and expecting boolean response
*/
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Boolean.class);
if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
/**
* Printing Resoponse body
*/
System.out.println("test makes call successful :: " + response.getBody());
} else {
System.err.println("Error while test making call");
}
} catch (HttpStatusCodeException exception) {
/**
* to get status code for exception
*/
System.err.println(exception);
} catch (Exception e) {
/**
* Other exception
*/
System.err.println(e);
}
}
the above method will send request to following method in which @corsorigin is added as this for demo
@GetMapping("simpleget")
@CrossOrigin /** to allow request from any origin **/
public boolean simpleget() {
System.out.println("Thank you i received the hit");
return true;
}
for testing we can hit following url
http://localhost:port/triggersimpleget
To send get request with query parameter we can use following code
@GetMapping("hitwithqueryparam")
public void test(@RequestParam(value = "input1") String input1, @RequestParam(value = "input2") String input2) {
/**
* This method takes two input and calls testtwo endpoint passing query parameter
*/
try {
String url = "http://localhost:1818/acceptqueryparam?string1={firstinput}&string2={secondinput}";
HashMap<String, String> queryparams = new HashMap<String, String>();
/**
* Building and mapping query parameter
*/
queryparams.put("firstinput", input1); //will map values into the curly bracket as query param {}
queryparams.put("secondinput", input2);//will map values into the curly bracket as query param {}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Boolean.class,queryparams);
if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
/**
* Printing Resoponse body
*/
System.out.println("test makes call successful :: " + response.getBody());
} else {
System.err.println("Error while test making call");
}
} catch (HttpStatusCodeException exception) {
System.err.println(exception);
} catch (Exception e) {
System.err.println(e);
}
}
Below method will accept and print the query param
@GetMapping("acceptqueryparam")
@CrossOrigin
public boolean acceptqueryparam(@RequestParam(value = "string1") String string1,
@RequestParam(value = "string2") String string2) {
/**
* endpoint accepts query parameter as input and prints
*/
if (string1.length() != 0 && string2.length() != 0) {
System.out.println("Receieved " + string1 + " and " + string2);
return true;
}
return false;
}
To trigger post request following is the code
@GetMapping("triggersimplepost")
public void triggersimplepost() {
try {
/**
* request will be sent to url
*/
String url = "http://localhost:1818/simplepost";
HashMap<String, String> reqpayload = new HashMap<String, String>();
reqpayload.put("user", "john");
/**
* setting header to accept json
*/
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
/**
* Adding request payload.
*/
HttpEntity<?> httpEntity = new HttpEntity<>(reqpayload , headers);
/**
* exchange( urltobehit, httpmethod, header, response, urivaraibles
* we are sending get request and expecting boolean response
*/
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Boolean.class);
if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
/**
* Printing Resoponse body
*/
System.out.println("test makes call successful :: " + response.getBody());
} else {
System.err.println("Error while test making call");
}
} catch (HttpStatusCodeException exception) {
/**
* to get status code for exception
*/
System.err.println(exception);
} catch (Exception e) {
/**
* Other exception
*/
System.err.println(e);
}
}
Here is brief explnation
here we are passing the request payload by passing the map, request boy is built
HashMap<String, String> reqpayload = new HashMap<String, String>();
reqpayload.put("user", "john");
Adding headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
Combining headers and body
HttpEntity<?> httpEntity = new HttpEntity<>(reqpayload , headers);
Triggering a post call with request body and expected response as boolean
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Boolean.class);
following method accepts the data and reads the same
@PostMapping("simplepost")
@CrossOrigin
public boolean simplepost(@RequestBody Map<String,String> req) {
System.out.println("Get request body and reading json body with key user :: "+req.get("user"));
return true;
}
RestTemplatemaybe it can help you