{"id":19164014,"url":"https://github.com/souravcoder1/spring-boot-restful-web-service","last_synced_at":"2026-04-24T12:07:18.454Z","repository":{"id":162681550,"uuid":"259349883","full_name":"souravCoder1/spring-boot-restful-web-service","owner":"souravCoder1","description":"A detailed Standalone RESTful web service example application with the use of Spring Boot framework","archived":false,"fork":false,"pushed_at":"2020-04-29T16:30:48.000Z","size":357,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-17T19:51:15.673Z","etag":null,"topics":["spring-boot"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/souravCoder1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-04-27T14:26:50.000Z","updated_at":"2020-04-29T16:30:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"3a82711b-56d9-42b1-b422-0e859107227a","html_url":"https://github.com/souravCoder1/spring-boot-restful-web-service","commit_stats":null,"previous_names":["souravcoder1/spring-boot-restful-web-service"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/souravCoder1/spring-boot-restful-web-service","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/souravCoder1%2Fspring-boot-restful-web-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/souravCoder1%2Fspring-boot-restful-web-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/souravCoder1%2Fspring-boot-restful-web-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/souravCoder1%2Fspring-boot-restful-web-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/souravCoder1","download_url":"https://codeload.github.com/souravCoder1/spring-boot-restful-web-service/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/souravCoder1%2Fspring-boot-restful-web-service/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32222535,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-24T10:26:35.452Z","status":"ssl_error","status_checked_at":"2026-04-24T10:25:27.643Z","response_time":64,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["spring-boot"],"created_at":"2024-11-09T09:17:33.699Z","updated_at":"2026-04-24T12:07:18.431Z","avatar_url":"https://github.com/souravCoder1.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring-boot-restful-web-service\n\nA detailed Standalone RESTful web service example application with the use of Spring Boot framework\n\nSpring Boot RESTful Web Service Example\n=======================================\nThe main purpose of this sample project is to demonstrate the capabilities of spring boot.\nBut additionally, I want to show challenging problems that can occur during the development\nwhile using the Spring Boot. \n\nFirst goal is to show how it is easy to start a web service with embedded tomcat and embedded\nH2 database. This is the main goal of the project.\n\nSecondly, we are using Spring and I have used dependency injection. But what is a challenging\nproblem about dependency injection. Assume that you have two implementations ready for one\nimplementation, how are you going to select the implementation? I'll explain several ways but\nalso I'll demonstrate how we can select our implementation via external configuration so that\nwe can update our configuration and don't need to touch the code, restart our jar file and\nthat's all.\n\nThirdly, I also have demonstrated how to use Java Application Configuration within the double\nimplementation for the single interface scenario I've explained above.\n\nLastly, I will explain all the deployment details, the main configuration of the whole project\nincluding H2 database configuration.\n\nMoreover, I will also demonstrate how you are going to test your RESTful application with\nPostman tool.\n\nTOC\n---\n- [0 Prerequisite And Demo App](#0-prerequisite-and-demo-app) \u003cbr/\u003e\n- [1 About Spring Boot](#1-about-spring-boot) \u003cbr/\u003e\n- [2 Create Spring Boot Project With Maven](#2-create-spring-boot-project-with-maven) \u003cbr/\u003e\n- [3 Spring Boot Dependencies](#3-spring-boot-dependencies) \u003cbr/\u003e\n- [4 Making Uber Jar](#4-making-uber-jar) \u003cbr/\u003e\n- [5 Project Overview](#5-project-overview) \u003cbr/\u003e\n- [6 External Configuration Example](#6-external-configuration-example) \u003cbr/\u003e\n- [7 Application Properties](#7-application-properties) \u003cbr/\u003e\n- [8 H2 Database Preparation](#8-h2-database-preparation) \u003cbr/\u003e\n- [9 Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n  * [9-a- Test](#9-a-test) \u003cbr/\u003e\n  * [9-b- List](#9-b-list) \u003cbr/\u003e\n  * [9-c- Create](#9-c-create) \u003cbr/\u003e\n  * [9-d- Retrieve](#9-d-retrieve) \u003cbr/\u003e\n  * [9-e- Update](#9-e-update) \u003cbr/\u003e\n  * [9-f- Delete](#9-f-delete) \u003cbr/\u003e\n- [10 Building And Running The Standalone Application](#10-building-and-running-the-standalone-application) \u003cbr/\u003e\n\n 0 Prerequisite And Demo App\n----------------------------\nTo use this project, you are going to need;\n\n- Java JDK 8 (1.8)\n- Maven compatibile with JDK 8\n- Any Java IDE\n- [Postman tool](https://www.getpostman.com/) (optional, will be used for testing web service)\n\nWe are going to build a demo app named as consultant-api. This will be a simple web service with\nbasic CRUD operations. I'm going to demonstrate default and external configuration, how to use\nmultiple implementation and autowire them within the code and outside the code with an external\nconfiguration file. Our app will be a standalone application that we can use independently, and\nwe are going to use an embedded tomcat, an embedded H2 database.\n\n[Go back to TOC](#toc)\n\n\n 1 About Spring Boot\n--------------------\nWhenever there is a new framework on the town, you must think two thinks. One, why should I use this\nframework which means \"what are the benefits of this framework\", also can be interpreted like \"what\nthis framework solves?\". Two, \"When should I use this framework?\", also can be interpreted as \"on\nwhich specific scenarios this framework is useful\" or can be simplified as \"what is the problem domain\nof this framework?\".\n\nWhen we make a web service with spring framework, we have to generate a war file, we need to configure\nweb.xml, and also if we are going to use the connection pool, the configuration is costly. All of these\nincreases the cost of time. So instead of writing your code, doing your development, you a lot of time\nis wasted during the configuration. This is where Spring Boot comes to the action. Spring Boot simplifies\nconfiguration, reduces boilerplate code that puts no any value to your software development.\n\nSo, what Spring Boot solves is the time lost for the configuration. For example, you can create a web\nservice with Spring Boot that runs on an embedded Tomcat server which is automatically configured and\nyou don't have to deal with the configuration. You can do all your configuration parameters via default\napplication properties. Also you can connect to an H2 embedded database, same applies for the\nconfiguration here. \n\nSecondly, you don't have to generate a war file. All Spring Boot applications run as a standalone java\njar file. Where is it useful then? If you are using a microservice architecture which runs especially\non a cloud (but not necessarily), then you can easily do your development via Spring Boot. In my opinion,\nSpring Boot is one of the best frameworks you should use on such a scenario and architecture. You can\neasily create simple web services, put them inside a Docker container (which is not a part of this\ntutorial) and run them on the Amazon Web Services or on any cloud environment.\n\nAdditionally, you don't have to track the versioning of your dependencies. Normally, if you are using\na version of spring, then you have to use the appropriate versions of your other dependencies which are\ndependent to the main dependency. With Spring Boot, you don't have to check out what version you have\nto use for Jackson which is compatible with the version of Jersey. You don't even need to write the\nversion of your dependencies. I'm going to demonstrate all of these benefits.\n\n[Go back to TOC](#toc)\n\n\n 2 Create Spring Boot Project With Maven\n----------------------------------------\nWhat we need to setup a Spring Boot project. However there are other ways (like spring initializer),\nI'll go with setting up our project with maven.\n\nBecause that we are creating a web application here, we will first create a maven project with web\napplication archetype, then we will add the spring boot dependencies;\n\nYou can use the following maven command to create a project. In this project, I've used this exact\nmaven command to create our project;\n\n```\nmvn archetype:generate -DgroupId=com.levent.consultantapi -DartifactId=consultant-api  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false\n```\n\n[Go back to TOC](#toc)\n\n\n 3 Spring Boot Dependencies\n---------------------------\nHow we make our project a Spring Boot project. We simply define a parent project in our POM file\njust as below;\n\n```\n\u003c!-- Spring Parent Project --\u003e\n\u003cparent\u003e\n\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n\t\u003cartifactId\u003espring-boot-starter-parent\u003c/artifactId\u003e\n\t\u003cversion\u003e1.3.1.RELEASE\u003c/version\u003e\n\u003c/parent\u003e\n```\n\nThen our dependencies aware of our Spring Boot project. Then for example, if we are going to create\na web application which is true, then we add the Spring Boot Starter dependencies. On this project,\nit is vital for us to add the dependency below;\n\n```\n\u003c!-- Spring boot starter web: integrates and auto-configures  --\u003e\n\u003cdependency\u003e\n\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n\t\u003cartifactId\u003espring-boot-starter-web\u003c/artifactId\u003e\n\u003c/dependency\u003e\n```\n\nSee that we did not use the version attribute of the dependency block. That's what Spring Boot is \ngoing to handle but just adding this starter dependency, we will have all what we need like Jersey,\nJackson, and the rest. Also the versioning is managed by Spring Boot, we don't have to check if\nthe versions of our transitive dependencies are compatible with each other or not.\n\nMoreover, in this project we will need an H2 embedded database. So we are going to add the following\ndependency to our dependencies block;\n\n```\n\u003c!-- H2 Embedded Database --\u003e\n\u003cdependency\u003e\n\t\u003cgroupId\u003ecom.h2database\u003c/groupId\u003e\n\t\u003cartifactId\u003eh2\u003c/artifactId\u003e\n\u003c/dependency\u003e\n```\n\nThe last thing additional to our dependencies in the POM file is to use the @SpringBootApplication\nannotation on our [EntryPoint](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/EntryPoint.java) class.\n\nWith this, Spring Boot configuration is complete.\n\n[Go back to TOC](#toc)\n\n\n 4 Making Uber Jar\n------------------\n\nWhen we are developing a web service, let's say, using Jersey framework within the Spring context,\nwe make a .war file and upload it to a container. However, Spring Boot is a containerless framework.\nSo we do not need any web container, which means also we won't need to generate a .war file. What\nwe have to do is pack all the libraries and frameworks we are using in our project into a big jar\nfile, the Uber Jar (a.k.a. Fat Jar).\n\nTo do so, our build tool maven has a plugin named as Maven Shade Plugin. We are going to define\nit within the build block of our POM file. You can see the sample build block as below;\n\n```\n\u003cbuild\u003e\n  \u003cplugins\u003e\n    \u003cplugin\u003e\n      \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n      \u003cartifactId\u003emaven-compiler-plugin\u003c/artifactId\u003e\n      \u003cconfiguration\u003e\n        \u003csource\u003e1.8\u003c/source\u003e\n        \u003ctarget\u003e1.8\u003c/target\u003e\n      \u003c/configuration\u003e\n    \u003c/plugin\u003e\n    \u003cplugin\u003e\n      \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n      \u003cartifactId\u003emaven-shade-plugin\u003c/artifactId\u003e\n      \u003cexecutions\u003e\n        \u003cexecution\u003e\n          \u003cphase\u003epackage\u003c/phase\u003e\n          \u003cgoals\u003e\n            \u003cgoal\u003eshade\u003c/goal\u003e\n          \u003c/goals\u003e\n          \u003cconfiguration\u003e\n            \u003ctransformers\u003e\n              \u003ctransformer \nimplementation=\"org.apache.maven.plugins.shade.resource.AppendingTransformer\"\u003e\n                \u003cresource\u003eMETA-INF/spring.handlers\u003c/resource\u003e\n              \u003c/transformer\u003e\n              \u003ctransformer\nimplementation=\"org.springframework.boot.maven.PropertiesMergingResourceTransformer\"\u003e\n                \u003cresource\u003eMETA-INF/spring.factories\u003c/resource\u003e\n              \u003c/transformer\u003e\n              \u003ctransformer\nimplementation=\"org.apache.maven.plugins.shade.resource.AppendingTransformer\"\u003e\n                \u003cresource\u003eMETA-INF/spring.schemas\u003c/resource\u003e\n              \u003c/transformer\u003e\n              \u003ctransformer\nimplementation=\"org.apache.maven.plugins.shade.resource.ServicesResourceTransformer\" /\u003e\n              \u003ctransformer implementation=\"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer\"\u003e\n                \u003cmainClass\u003ecom.levent.consultantapi.EntryPoint\u003c/mainClass\u003e\n              \u003c/transformer\u003e\n            \u003c/transformers\u003e\n          \u003c/configuration\u003e\n        \u003c/execution\u003e\n      \u003c/executions\u003e\n    \u003c/plugin\u003e\n  \u003c/plugins\u003e\n\u003c/build\u003e\n```\n\nAs you can see, there are two plugins used in the plugins block of the build block above. I've\nused maven compiler plugin so that I can define the source and destination version. The other\nplugin is the Maven Shade Plugin, which we use in order to pack our Uber Jar.\n\nIn the Maven Shade Plugin block, we define our mainClass. Because our application is a standalone\nJava application, we have to define the Entry Point, the starter class of our appliacation. The\nname of this class is arbitrary.\n\nYou can check out the full POM file: [Project Object Model](https://github.com/bzdgn/simple-grizzly-standalone-restful-webservice-example/blob/master/pom.xml)\n\n[Go back to TOC](#toc)\n\n\n 5 Project Overview\n-------------------\n\nOur project consist of several layers. For the simplicity, if we exclude the [EntryPoint](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/EntryPoint.java) class \nwhich is located at the top level package, we have the following packages;\n\n- [controller package](https://github.com/bzdgn/spring-boot-restful-web-service-example/tree/master/src/main/java/com/levent/consultantapi/controller)\n- [service package](https://github.com/bzdgn/spring-boot-restful-web-service-example/tree/master/src/main/java/com/levent/consultantapi/service)\n- [repository package](https://github.com/bzdgn/spring-boot-restful-web-service-example/tree/master/src/main/java/com/levent/consultantapi/repository)\n- [model package](https://github.com/bzdgn/spring-boot-restful-web-service-example/tree/master/src/main/java/com/levent/consultantapi/model)\n\nYou can see the logical representation below of these packages;\n\n![project-overview](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/01_diagram.png)\n\nController package has one controller class which is [ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java). With [ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java),\nyou can define all the RESTful methods. This class need to use a Service Layer implementation, thus\n[ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java) has a [ConsultantService](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/ConsultantService.java) interface and we are using the @Autowired annotation \nso that Spring context is going to find the appropriate implementation. In our case, we have only \none Service implementation which is [ConsultantServiceImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/consultant/impl/ConsultantServiceImpl.java).\n\nYou can also see the [InfoService](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/InfoService.java) interface wrapped inside the [ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java) class. It has also\nmarked with the @Autowired annotation. I'll explain it later for the simplicity but our first focus\nin this project overview is to explain the main structure of this simple application.\n\nAt service layer, we have the interface [ConsultantService](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/ConsultantService.java) and one implementation that fits with this\ninterface: [ConsultantServiceImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/consultant/impl/ConsultantServiceImpl.java). You will see that [ConsultantServiceImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/consultant/impl/ConsultantServiceImpl.java) has a [ConsultantRepository](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/repository/ConsultantRepository.java)\ninterface and that interface also marked with the @Autowired annotation.\n\nAt repository layer, we have a different situation. There are two implementation fits with this\n[ConsultantRepository](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/repository/ConsultantRepository.java) interface;\n- [ConsultantStubImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/repository/impl/ConsultantStubImpl.java)\n- [ConsultantJPARepositoryImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/repository/impl/ConsultantJPARepositoryImpl.java)\n\nSo, how Spring handles if there are two implementation classes those implements one interface with\n@Autowired annotation? How spring is going to select the implementation candidates? Normally, Spring\nis going to get confused, throw Exceptions and you will find the following message inside the stack\ntrace;\n\n```\nCaused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.levent.consultantapi.repository.ConsultantRepository] is defined: expected single matching bean but found 2: consultantJPARepositoryImpl,consultantStubImpl\n```\n\nBecause that there are two candidate implementation for one single interface, autowiring functionality\nof Spring Context will fail. The solution is to use @Primary annotation. You can see it inside the\n[ConsultantJPARepositoryImpl](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/repository/impl/ConsultantJPARepositoryImpl.java). When there are multiple implementation for the same interface marked with\n@Autowired annotation, you have to use @Primary on one of the implementations.\n\nBut what if we want to have two different implementations and we want to change which implementation\nto use, without changing the code ? Then we can use an external configuration, which I'm going to\nexplain it on next chapter.\n\n[Go back to TOC](#toc)\n\n\n 6 External Configuration Example\n---------------------------------\n\nNormally, Spring Boot configuration is defined with [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties). It is default location can be either under\nsrc/main/resources folder, or under a subfolder of current directoy named with \"config\". I'm using the second way, created\na config folder under the project, and put the main [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties) file there. \n\nHowever, I also want to have the common properties on [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties) file, and in addition to it, for specific\npurposes, I want to use a secondary properties folder. In this chapter, I'm going to demonstrate how to use externalized\ncustom properties file.\n\nBut first, let's go back to our previous problem that which I've talked about. The scenario is as follows: I've two or multiple\nimplementations for a single interface with @Autowired annotation, and I don't want to do any code change when I switch between \nthe implementations. Spring's solution for that was using the @Primary annotation so that Spring Context is not going to throw an  exception because that it does not know which implementation to use.\n\nHere is my solution;\n\n- I create an externalized configuration file named as [implementation.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/implementation.properties)\n- I've created [AppConfig](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/config/AppConfig.java) class to read the custom configuration file\n\nIf you look at to [AppConfig](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/config/AppConfig.java) class under the config package, you will see that the class file is marked with two annotations.\nFirst it is marked with @Configuration annotation because it needs to be done before the injection of the autowired variable.\nAnd secondly, it is marked with @PropertySource, so that we are going to point out which specific property file we are going\nto use.\n\nThe process is as follows;\n\n- Spring context searchs for the @Configuration annotation, finds the [AppConfig](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/config/AppConfig.java)\n- Via [AppConfig](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/config/AppConfig.java), reads the [implementation.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/implementation.properties) file\n- The greeter.implementation property's value is loaded to the impl variable in the [AppConfig](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/config/AppConfig.java)\n- Via the #getImplementationFromPropertiesFile method, the implementation bean is created based on the config file\n- The context has the right implementation based on the configuration\n- During the creation of the [ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java) class, Spring Context finds the autowired field: greeter which is an interface: [InfoService](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/service/InfoService.java)\n- The implementation bean is autowired to this greeter field\n\nYou can see the overview of this process via the diagram below;\n\n![process-overview](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/02_diagram.png)\n\n[Go back to TOC](#toc)\n\n\n 7 Application Properties\n-------------------------\n\nSpring Boot solves our problem with automatic configuration as we use an embedded Tomcat and an embedded H2\ndatabase but how are we going to specify the running port of the Tomcat container, the target database, \nconnection pool parameters and so on?\n\nSpring Boot provides a default configuration properties file called as [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties). Within this file\nthere are hundreds of configuration parameters we can use. You can see the detailed parameter list via following\nlink;\n\n[Spring Boot Application Properties Reference](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)\n\nThe default locations of the application.properties file is either somewhere within the classpath, for example\nunder src/main/resources in a maven project, or a inside config folder under current working directory. It is\nbetter to put the file under config folder which will make it easy to deploy inside a docker container, but\nthe choice is yours. I place [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties) under config folder.\n\nBecause that we are using a server, H2 database, a datasource, a db connection pool and lastly, hibernate,\nwe should define parameters in this [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties) file, based on the [documented reference list](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html).\n\nLet's take a detailed look;\n\n- Server Configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;The default configuration port is 8080, however we may want to change this. Thus I add the port configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;as below;\n```\n#server port\nserver.port=8080\n```\n    \n- H2 Database Configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbspWe also need to specify whether the console is activated, so that we can use H2 database via the console, create\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;our tables and initialize our db entries.\n    \n```\n#H2 configuration\nspring.h2.console.enabled=true\nspring.h2.console.path=/h2\n```\n\n- DataSource Configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;Instead of writing a connection string, we are defining the parameters via our properties file as below;\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;Notify that we defined our database as a file and the name of the database is \"consultantapi\". We are going to use it when\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;we need to connect the database via the console;\n\n```\n#Data source configuration\nspring.datasource.url=jdbc:h2:file:~/consultantapi\nspring.datasource.username=sa\nspring.datasource.password=\nspring.datasource.driver-class-name=org.h2.Driver\n```\n\n- Connection Pool Configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Here we define the connection pool parameters;\n\n```\n#DB Pool conf\nspring.datasource.max-active=10\nspring.datasource.max-idle=8\nspring.datasource.max-wait=10000\nspring.datasource.min-evictable-idle-time-millis=1000\nspring.datasource.min-idle=8\nspring.datasource.time-between-eviction-runs-millis=1\n```\n\n- Hibernate Configuration\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;We don't want Hibernate to delete our database entries on every restart of our server, so we need to configure as below;\n\n```\n#Hibernate Config\nspring.jpa.hibernate.ddl-auto=false\t\t#false for persistent database\n```\n\nYou can check our project's application properties file via here: [application.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/application.properties)\n\n[Go back to TOC](#toc)\n\n\n 8 H2 Database Preparation\n--------------------------\nBefore using our database implementation instead of using our stub implementation, we need to prepare our table\nand initial entries within the database. We have two things to do;\n\n1. We need to create a CONSULTANT table to store our consultant model, defined in [Consultant](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/model/Consultant.java) class.\n2. We need to insert some trivial entries to the table.\n\nThe related SQL commands are located under the [consultant.sql](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/resources/consultant.sql)\n\nAs it is written on previous chapter, we have defined our H2 console with \"/h2\" postfix. Also we have defined our server\nport as \"8080\", so connection url will be [http://localhost:8080/h2/](http://localhost:8080/h2/). We can connect our H2\ndatabase console as below;\n\n![H2-console](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/03_diagram.png)\n\nAfter connecting to the database via the console, we can easily run our sql commands defined in [consultant.sql](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/resources/consultant.sql) as below;\n\n![H2-console](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/04_diagram.png)\n\nNow our database is ready to go!\n\n[Go back to TOC](#toc)\n\n\n 9 Sending And Receiving JSONs With Postman\n-------------------------------------------\n\nIn this part, I'm going to demonstrate all operations defined in our [ConsultantController](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/src/main/java/com/levent/consultantapi/controller/ConsultantController.java) class and how to test them\neither with our web browser or with the [Postman tool](https://www.getpostman.com/). We can test our GET methods with any web browser but for\noperations that use HTTP POST, PUT, DELETE methods, we cannot execute them with a simple web browser, so I'm\ngoing to use [Postman tool](https://www.getpostman.com/) for that.\n\nYou can download Postman via [this link](https://www.getpostman.com/)\n\nI've provided CRUD operations within postman, so that you can load all the prepared operations in Postman tool. You can\nfind the content under misc directory;\n\n[Postman Collection](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/misc/Consultant_API.postman_collection.json)\n\n 9-a Test\n---------\n```\nSub Path: /test\nFull URL: http://localhost:8080/api/v1/test\nMethod:   GET\nSends:    N/A\nReceives: Text\nSample Input: N/A\nSample Output; \n\nConsultant-Api Version: 1.0.0 Written by: Levent Divilioglu\n```\n\nWe can simply use our web browser and receive the text output as below;\n\n![test-sample](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/05_test.png)\n\nBut take into the consideration that this response based on which implementation we define on our custom configuration\nfile which is: [implementation.properties](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/config/implementation.properties).\n\nYou can select one of the four different implementations via the configuration file and see the results. For\nmore information, you can go back to the [6 External Configuration Example](#6-external-configuration-example) section.\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n 9-b List\n---------\n```\nSub Path: /consultants\nFull URL: http://localhost:8080/api/v1/consultants\nMethod:   GET\nSends:    N/A\nReceives: JSON\nSample Input: N/A\nSample Output;\n[{\n\t\"id\": 1,\n\t\"firstName\": \"Levent\",\n\t\"lastName\": \"Divilioglu\",\n\t\"age\": 36,\n\t\"client\": null,\n\t\"assigned\": false\n},\n{\n\t\"id\": 2,\n\t\"firstName\": \"Altug\",\n\t\"lastName\": \"Timuroglu\",\n\t\"age\": 41,\n\t\"client\": \"Altinorda IT\",\n\t\"assigned\": true\n},\n{\n\t\"id\": 3,\n\t\"firstName\": \"Bugra\",\n\t\"lastName\": \"Cengizoglu\",\n\t\"age\": 37,\n\t\"client\": \"KizilTug TECH\",\n\t\"assigned\": true\n}]\n```\n\nAgain we can use web browser to get the results as below;\n\n![list-sample](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/06_list.png)\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n 9-c Create\n-----------\n```\nSub Path: /consultants\nFull URL: http://localhost:8080/api/v1/consultants\nMethod:   POST\nSends:    JSON\nReceives: JSON\nSample Input;\n{\n\t\"id\": 4,\n\t\"firstName\": \"John\",\n\t\"lastName\": \"Doe\",\n\t\"age\": 99,\n\t\"client\": \"Example Tech\",\n\t\"assigned\": true\n}\nSample Output;\n{\n    \"id\": 4,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"age\": 99,\n    \"client\": \"Example Tech\",\n    \"assigned\": true\n}\n```\n\nThis time, the operation we are using is POST, so we cannot do that with our browser, we have to use our tool\nPostman. Here is how I create my HTTP request;\n\n![create-sample](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/07_create_input.png)\n\nWith the '+' sign above on the Postman screen, we can create our HTTP request. Then we select HTTP Method as POST.\nWe paste the URL, select \"raw\", and JSON for our input content. Then we select the \"body\" tag, and paste our content\nwhich we want to POST. Under the second half of the screen, on the \"body\" tag, we will retrieve our JSON response.\n\nAs you can see, the created content is returned. We can also test the result using our list service via postman (or\nweb browser);\n\n![create-sample-test](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/08_create_input_test.png)\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n 9-d Retrieve\n-------------\n```\nSub Path: /consultants\nFull URL: http://localhost:8080/api/v1/consultants/{id}\nMethod:   GET\nSends:    N/A\nReceives: JSON\nSample Input: N/A\nSample Output;\n{\n    \"id\": 4,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"age\": 99,\n    \"client\": \"Example Tech\",\n    \"assigned\": true\n}\n```\n\nIt is a simple GET again, and let's use our browser for testing. We are going to use a path\nparameter which will be \"4\", the full path is as below;\n\n```\nhttp://localhost:8080/api/v1/consultants/4\n```\n\nThe output will be as follows;\n\n![retrieve-test](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/09_retrieve_test.png)\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n 9-e Update\n-----------\n```\nSub Path: /consultants\nFull URL: http://localhost:8080/api/v1/consultants/{id}\nMethod:   PUT\nSends:    JSON\nReceives: JSON\nSample Input;\n{\n\t\"id\": 4,\n\t\"firstName\": \"Jayne\",\n\t\"lastName\": \"Smith\",\n\t\"age\": 66,\n\t\"client\": \"Example New Company\",\n\t\"assigned\": true\n}\nSample Output;\n{\n    \"id\": 4,\n    \"firstName\": \"Jayne\",\n    \"lastName\": \"Smith\",\n    \"age\": 66,\n    \"client\": \"Example New Company\",\n    \"assigned\": true\n}\n```\nIn order to update, we are going to use PUT method, so we will use Postman again.\n\nFor update method, we have to give the id in the URL as a path parameter, so URL will be as below;\n\n```\nhttp://localhost:8080/api/v1/consultants/4\n```\n\n![update-sample](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/10_update_input.png)\n\nAs you can see, the updated content is returned. We can also test the result using our retrieve service via postman (or\nweb browser);\n\n![update-sample-test](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/11_update_input_test.png)\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n 9-f Delete\n-----------\n```\nSub Path: /consultants\nFull URL: http://localhost:8080/api/v1/consultants/{id}\nMethod:   DELETE\nSends:    N/A\nReceives: JSON\nSample Input: N/A\nSample Output;\n{\n    \"id\": 4,\n    \"firstName\": \"Jayne\",\n    \"lastName\": \"Smith\",\n    \"age\": 66,\n    \"client\": \"Example New Company\",\n    \"assigned\": true\n}\n```\n\nAgain, we will use Postman for DELETE operation. As it is shown above, we don't have to provide\na body for this operation, but we have to provide the id of the consultant to be deleted within the\nURL as below;\n\n```\nhttp://localhost:8080/api/v1/consultants/4\n```\n\n![delete-sample](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/12_delete_sample.png)\n\nAs you can see, the deleted content is returned. We can also test the result using our retrieve service via postman (or\nweb browser);\n\n![delete-sample-test](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/13_delete_sample_test.png)\n\n[Go back to Sending And Receiving JSONs With Postman](#9-sending-and-receiving-jsons-with-postman) \u003cbr/\u003e\n[Go back to TOC](#toc)\n\n\n 10 Building And Running The Standalone Application\n---------------------------------------------------\nNow we can demonstrate how to run our consultant-api as a standalone application. First we must build with the following\nmaven command;\n\n```\nmvn clean package\n```\n\nThis command is going to collect all the needed jars and pack them into an Uber Jar (a.k.a. fat jar). We can find this\nUber Jar under the \"target\" folder. The name of the file will be: \"consultant-api-1.0-SNAPSHOT.jar\"\n\nWe are going to take this file and copy it to another arbitrary folder. Remember that we also need two configuration files,\nthose that located under the config file. We will also copy those config files to our arbitrary folder.\n\nI copied all the files I mentioned above to the folder \"D:\\consultant-api\\\", the structure is as follows;\n\n```\nD:\\consultant-api\n       |\n       |___ consultant-api-1.0-SNAPSHOT.jar\n       |___ config\n              |______ application.properties\n              |______ implementation.properties\n```\n\nIf the structure of the arbitrary folder (here it is 'consultant-api'), then we can try to run our standalone application\nto see if it is working. Here is a successful output. For the simplicity, I'm not going to paste all the log output;\n\n```\nD:\\consultant-api\u003ejava -jar consultant-api-1.0-SNAPSHOT.jar\n\n  .   ____          _            __ _ _\n /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\n( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\n \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\n  '  |____| .__|_| |_|_| |_\\__, | / / / /\n =========|_|==============|___/=/_/_/_/\n :: Spring Boot ::         (v1.0-SNAPSHOT)\n\n2018-07-01 16:16:08.705  INFO 6616 --- [           main] com.levent.consultantapi.EntryPoint      : Starting EntryPoint v1.0-SNAPSHOT on LEVASUS with PID 6616 (D:\\consultant-api\\consultant-api-1.0-SNAPSHOT.jar started by Levent in D:\\consultant-api)\n2018-07-01 16:16:08.711  INFO 6616 --- [           main] com.levent.consultantapi.EntryPoint      : No active profile set, falling back to default profiles: default\n2018-07-01 16:16:08.785  INFO 6616 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27808f31: startup date [Sun Jul 01 16:16:08 CEST 2018]; root of context hierarchy\n2018-07-01 16:16:10.487  INFO 6616 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMet\n```\n\nThen we can test if our standalone application is working fine with our web browser;\n\n![standalone-test](https://github.com/bzdgn/spring-boot-restful-web-service-example/blob/master/ScreenShots/14_standalone_test.png)\n\nYes, our standalone application is working fine. We can easily deploy it in a Docker container, or just run it as\nit is.\n\n[Go back to TOC](#toc)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouravcoder1%2Fspring-boot-restful-web-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsouravcoder1%2Fspring-boot-restful-web-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouravcoder1%2Fspring-boot-restful-web-service/lists"}