![]() Since spring finds this library in class path, it invokes inbuilt MappingJackson2HttpMessageConverter converter to convert the response (List of objects) into JSON. This is due to the fact that we have included Jackson library in our project. You might be wondering how the response is sent as JSON string, and the Content-Type header in response confirms that. Click on Send, you will receive list of all users.Īlso notice the HTTP 200 response. Notice that we did not specify any HTTP header here. Open POSTMAN tool, select request type, specify the operation uri. We will write our own client in just few minutes. Deployed Application is available at To test this API, i will use an external client POSTMAN (An extension from CHROME). I am going to deploy it, in order to see things live and discuss each operation in detail. MediaType : With annotation, you can additionally, specify the MediaType to be produced or consumed (using produces or consumes attributes) by that particular controller method, to further narrow down the mapping.ĭeploy and Test this API, let’s dig deeper into how this thing worksĪt the at end of day, it’s just a plain controller class, part of a deploy-able application. Additionally, spring provides several support classes to help you implement something customized. -Retrieve Single = "/user/’].īasically,, ResponseEntity & are all you need to know to implement a REST API in Spring 4. Return new ResponseEntity>(users, HttpStatus.OK) Return new ResponseEntity>(HttpStatus.NO_CONTENT) //You many decide to return HttpStatus.NOT_FOUND -Retrieve All = "/user/", method = RequestMethod.GET) ![]() Import class HelloWorldRestController userService //Service which will do all data retrieval/manipulation work DELETE request to /api/user/ deletes all the users.DELETE request to /api/user/4 deletes the user with ID 4.PUT request to /api/user/3 with a user object as JSON updates the user with ID 3.POST request to /api/user/ with a user object as JSON creates a new user.GET request to /api/user/1 returns the user with ID 1.GET request to /api/user/ returns a list of users.I said possible, means Other’s may implement it in another way, still (or even more pure way) conforming to REST style. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |