{"id":97,"date":"2019-03-07T06:55:34","date_gmt":"2019-03-07T06:55:34","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=97"},"modified":"2020-07-26T08:13:13","modified_gmt":"2020-07-26T08:13:13","slug":"11-spring-boot-get-item-by-id","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/11-spring-boot-get-item-by-id\/","title":{"rendered":"Spring Boot &#8211; Get Item by Id"},"content":{"rendered":"<p>In this chapter, we are going to write the methods to get resources by Id.<\/p>\n<p>Remember that in the <a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/09-spring-boot-write-get-methods\/\">previous chapter(Write GET Methods)<\/a>, we wrote methods, to get all resources. For example, getAllUsers, getAllPosts and getAllLocation. But this time we would write methods for getUser, getPost and getLocation.<\/p>\n<p>This methods would take parameter, which is the id of the item to get.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">Get a Single User<\/a><\/li>\n<li><a href=\"#t2\">Get a Single Post<\/a><\/li>\n<li><a href=\"#t3\">Get a Single Location<\/a><\/li>\n<li>Test the Application<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Get a Single User<\/strong><\/h4>\n<p>So this method would be written in the UserService file. We call this method GetUser. On way to do this is to loop through the user and find a match for the passed in id parameter. But in this tutorial we would use the stream API.<\/p>\n<p>The method is as shown below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> User <span style=\"color: #0066bb; font-weight: bold;\">getUser<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    User user <span style=\"color: #333333;\">=<\/span> users<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">stream<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">filter<\/span><span style=\"color: #333333;\">(<\/span>t <span style=\"color: #333333;\">-&gt; id<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">()))<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findFirst<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">orElse<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> user<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: getUser Method using Stream API<\/p>\n<p>&nbsp;<\/p>\n<p>Now, we go to the UserController. We&#8217;ll write a method that handles a request to <em>\/User\/id<\/em>. For example the request to <em>\/User\/u1<\/em> should return the user with id of u1. Similarly request to <em>\/User\/u2<\/em> should return the user with id of u2. And so on.<\/p>\n<p>The method is shown below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/users\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> User <span style=\"color: #0066bb; font-weight: bold;\">getUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUser<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Get a Single Post<\/strong><\/h4>\n<p>We then write the methods to return a single post. It follows the same pattern as returning a single user.<\/p>\n<p>The method for the PostService is given below;<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> Post <span style=\"color: #0066bb; font-weight: bold;\">getPost<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    \tPost post <span style=\"color: #333333;\">=<\/span> posts<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">stream<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">filter<\/span><span style=\"color: #333333;\">(<\/span>t <span style=\"color: #333333;\">-&gt;<\/span> id<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">()))<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findFirst<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">orElse<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> post<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The method for the PostController is given below as well<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/posts\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Post <span style=\"color: #0066bb; font-weight: bold;\">getPost<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> postService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getPost<\/span><span style=\"color: #333333;\">(<\/span> id<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Get a Single Location<\/strong><\/h4>\n<p>In the same way, we write the getLocation method for the LocationService and LocationController files.<\/p>\n<p>&nbsp;<\/p>\n<p>Put the code below in the LocationService class<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> Location <span style=\"color: #0066bb; font-weight: bold;\">getLocation<\/span><span style=\"color: #333333;\">(<\/span>String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    Location location <span style=\"color: #333333;\">=<\/span> locations<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">stream<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">filter<\/span><span style=\"color: #333333;\">(<\/span>t <span style=\"color: #333333;\">-&gt;<\/span> id<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">()))<\/span>\r\n\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findFirst<\/span><span style=\"color: #333333;\">()<\/span>\r\n\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">orElse<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> location<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Put the code below in the LocationController class<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@RequestMapping<\/span><span style=\"color: #333333;\">(<\/span>value <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\/locations\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Location <span style=\"color: #0066bb; font-weight: bold;\">getLocation<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> String id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocation<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>4. Test the Application<\/strong><\/h4>\n<p>Now, it&#8217;s time to test the application.<\/p>\n<p>So right-click on the application. Choose Run As &gt; Spring Boot Application<\/p>\n<p>Then Open the browser and visit the following urls:<\/p>\n<ul>\n<li>http:\/\/localhost:8080\/users\/u1<\/li>\n<li>http:\/\/localhost:8080\/posts\/p1<\/li>\n<li>http:\/\/localhost:8080\/locations\/l1<\/li>\n<\/ul>\n<p>If you have any challenges, please mention it in the comment box below.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this chapter, we are going to write the methods to get resources by Id. Remember that in the previous chapter(Write GET Methods), we wrote &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":98,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/97\/revisions\/233"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media\/98"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}