How To Display Error Message In Restapi Services
In Spring REST client, The RestTemplate is the core grade for client-side access to Jump RESTful web services. Information technology communicates HTTP server using RESTful constraints. It is very similar to other template classes in the Leap like JdbcTemplate and HibernateTemplate etc. In Spring, RestTemplate provides higher level implementation of respective HTTP methods such as GET, POST, PUT, DELETE, Head etc. Information technology provides the methods to communicate by using these HTTP methods with URI template, URI param, request object and response type as arguments.
In this example, nosotros will see how to consume JSON response. In my previous article we have seen Spring RESTful spider web services crud example. Earlier we have used postman REST UI based client to demonstrate all HTTP methods such as get, mail service, delete and put. But here we are going to consume Restful web services via RestTemplate of Spring Residue client.
RestTemplate Methods
RestTemplate provides college level methods that correspond to each of the six main HTTP methods that make invoking many RESTful services. In the following list has methods provided past Bound RestTemplate for each http methods.
Method | Jump RestTemplate'due south method |
Get | getForObject, getForEntity |
Post | postForObject(String url, Object request, Class responseType, String…? uriVariables) postForLocation(String url, Object asking, String…? urlVariables), |
Put | put(String url, Object request, String…?urlVariables) |
Delete | delete() |
Head | headForHeaders(String url, String…? urlVariables) |
Options | optionsForAllow(Cord url, String…? urlVariables) |
As per every bit proper name of these methods of RestTemplate class betoken which HTTP method they implement internally and second part of the method name indicates what is return past these methods. For example, getForObject() will perform a HTTP Go activeness on the server, and convert the HTTP response into an object of given type and return it to the client. And other method postForLocation() will do a HTTP Mail service action on the server, converting the given object into a HTTP request, and returns the response HTTP Location header where the newly created object can exist constitute.
Permit's meet these RestTemplate'due south methods with examples.
Hither I am using Spring Restful web services Grime JSON Example of my previous post.
In this example, we are using @GetMapping , @PostMapping , @PutMapping and @DeleteMapping annotations, these annotations are introduced as of Jump 4.3 version in parallel of @RequestMapping annotation with Http Methods every bit beneath.
@GetMapping = @RequestMapping + Http GET method @PostMapping = @RequestMapping + Http POST method @PutMapping = @RequestMapping + Http PUT method @DeleteMapping = @RequestMapping + Http DELETE method
HTTP GET Method Instance
For API Get method telephone call nosotros can use either getForObject() or getForEntity() method of RestTemplate.
Jump REST API GET Lawmaking:
@GetMapping("/business relationship/{accountId}") public Account get (@PathVariable Long accountId){ return accountService.get(accountId); }
We could define same above API call equally below:
@RequestMapping(method=RequestMethod.GET, value = "/account/{accountId}", produces = MediaType.APPLICATION_JSON_VALUE) public Business relationship get (@PathVariable Long accountId){ return accountService.get(accountId); }
This telephone call binds to convert on JSON media type simply with the @RequestMapping annotation. If y'all want flexible render media type either JSON or XML based on the message converter available on the classpath of the awarding then it's recommend do not define produces holding asking mapping notation.
Jump RestTemplate go Method :
/** * */ package com.doj.restclient.account; import org.springframework.web.client.RestTemplate; /** * @writer Dinesh.Rajput * */ public form SpringRestClient { private static RestTemplate restTemplate = new RestTemplate(); private static terminal String baseURL = "http://localhost:8080/restapi/"; /** * @param args */ public static void main(String[] args) { //Read Account details for a given accountId = i using GET method of RestTemplate Account accountDetail = restTemplate.getForObject(baseURL+"account/one", Account.class); System.out.println("Account Detail for given AccountId : " +accountDetail); } }
Output on console:
Later on running above code, you will get beneath output:
Account Particular for given AccountId : Account [accountId=one, name=Dinesh Rajput, city=Noida, balance=212.33]
HTTP Mail service Method Instance
For Http POST method calling we can utilize either postForObject() or postForLocation() method of RestTemplate.
Bound Rest API Code post method:
@PostMapping("/account") public Business relationship create (@RequestBody Business relationship account){ return accountService.create(account); }
Spring RestTemplate post Method call:
/** * */ parcel com.doj.restclient.business relationship; import org.springframework.spider web.client.RestTemplate; /** * @author Dinesh.Rajput * */ public class SpringRestClient { private static RestTemplate restTemplate = new RestTemplate(); individual static concluding String baseURL = "http://localhost:8080/restapi/"; /** * @param args */ public static void chief(String[] args) { //Create Account using Postal service method of RestTemplate Business relationship account = new Account("Arnav Rajput", "Noida", 312.33); account = restTemplate.postForObject(baseURL+"account", account, Business relationship.form); System.out.println("Added account: " +account); } }
Output on panel:
After running above code, you will become below output:
Added business relationship: Account [accountId=three, name=Arnav Rajput, metropolis=Noida, remainder=312.33]
HTTP PUT Method Example
For Http PUT method calling we can utilise put() method of RestTemplate.
Spring Residue API Code put method:
@PutMapping("/account") public Account update (@RequestBody Account business relationship){ return accountService.update(account); }
Spring RestTemplate put Method call:
/** * */ package com.doj.restclient.business relationship; import org.springframework.web.customer.RestTemplate; /** * @author Dinesh.Rajput * */ public class SpringRestClient { private static RestTemplate restTemplate = new RestTemplate(); private static final String baseURL = "http://localhost:8080/restapi/"; /** * @param args */ public static void main(String[] args) { //Create Account using Mail service method of RestTemplate Account account = new Business relationship("Arnav Rajput", "Noida", 312.33); business relationship = restTemplate.postForObject(baseURL+"account", account, Account.grade); System.out.println("Added account: " +account); //Update Business relationship particular for given accountId = i using PUT method of RestTemplate Business relationship updateAccount = new Account("Anamika Rajput", "Noida", 123.33); updateAccount.setAccountId(1l); restTemplate.put(baseURL+"business relationship", updateAccount); Business relationship updatedAccount = restTemplate.getForObject(baseURL+"business relationship/1", Account.grade); Arrangement.out.println("Updated Business relationship: " +updatedAccount); } }
Output on console:
After running above code, you will go below output:
Added account: Account [accountId=1, proper name=Arnav Rajput, metropolis=Noida, rest=312.33]
Updated Account: Business relationship [accountId=1, proper name=Anamika Rajput, urban center=Noida, remainder=123.33]
HTTP DELETE Method Case
For Http DELETE method calling we tin can use delete() method of RestTemplate.
Bound Rest API Code delete method:
@DeleteMapping("/account/{accountId}") public void delete (@PathVariable Long accountId){ accountService.delete(accountId); }
Spring RestTemplate delete Method call:
/** * */ parcel com.doj.restclient.account; import coffee.util.List; import org.springframework.web.customer.RestTemplate; /** * @writer Dinesh.Rajput * */ public class SpringRestClient { private static RestTemplate restTemplate = new RestTemplate(); private static final String baseURL = "http://localhost:8080/restapi/"; /** * @param args */ public static void chief(String[] args) { List<Business relationship> accounts = restTemplate.getForObject(baseURL+"accounts", List.class); System.out.println("Total Accounts before DELETE phone call: "); for(Object acct : accounts){ System.out.println(acct); } //Delete Account for given accountId = 2 using Delete method of RestTemplate restTemplate.delete(baseURL+"account/2"); accounts = restTemplate.getForObject(baseURL+"accounts", List.class); System.out.println("Total Accounts after DELETE call: "); for(Object acct : accounts){ Arrangement.out.println(acct); } } }
Output on panel:
After running higher up code, you will go beneath output:
Total Accounts before DELETE call:
{accountId=i, name=Anamika Rajput, city=Noida, rest=123.33}
{accountId=2, proper noun=Anamika Rajput, city=Noida, balance=222.33}
{accountId=3, name=Arnav Rajput, city=Noida, residual=522.33}
Full Accounts subsequently DELETE telephone call:
{accountId=one, name=Anamika Rajput, urban center=Noida, remainder=123.33}
{accountId=3, name=Arnav Rajput, city=Noida, rest=522.33}
Download Source Code from GitHub.
- Bound RESTful spider web service CRUD Example
- Spring REST Client using RestTemplate Example
How To Display Error Message In Restapi Services,
Source: https://www.dineshonjava.com/spring-rest-client-resttemplate-consume-restful-web-service-example-json/
Posted by: greencomplew.blogspot.com
0 Response to "How To Display Error Message In Restapi Services"
Post a Comment