A Beginner’s Guide To Spring

Nikhil Bhatnagar
6 min readMar 31, 2020

A Framework is a software which is used for the building and development of software applications. It provides a platform on which developers can build programs for a specific platform. A platform may include predefined classes and functions that can be used to process input, manage hardware devices, and interact with different systems.

A framework is similar to an application programming interface, though technically it involves an API. Frameworks are mainly used for the “FULL STACK” development of a website or an application. Earlier, the connection of front end languages like HTML, CSS, etc with that of the back-end databases used to be a very challenging task.

But with the advent of frameworks, this problem became easier and today frameworks like .NET, android SDK and others are capable of developing a full website or an app on their own.

Spring is one such framework which was launched in 2002 and it was built with JAVA as it’s basis. It’s a powerful tool that eliminates the complexities of developing a web application in Java EE and provides a simpler interface to the developer.

Spring is light-weight, Aspect-Oriented, easily integrable with other frameworks, etc. In order to integrate these features, spring uses java annotations. One highlight of the spring framework is that spring applications are not IDE specific and with some modifications, they can run on any IDE that can run JAVA programs. Today, spring is known for its capability of easily developing APIs in java especially REST.

The Spring Logo

Starting with spring

Although Spring applications can run in any java supported IDE but it’s preferred to use Spring STS as it comes with services like in-built Tomcat etc. Just Google Spring STS download or spring.io/tools, download the spring sts and you are good to go.

Now, the “hello world” application of spring is the simulation of how the front-end application or client works with the back-end using spring. In order to simulate that we need to build our Spring Starter Application. (on opening the spring, we can select any workspace to work with. Even pre-existing eclipse workspaces also work.)

1. Open spring >file>Spring Starter Project

2. Change the name (and url if needed) >next>(type of project needed, here web)>finish

We make the type of project as web as we need to develop an application which runs on a server and which takes data from a client, say a website.

Now the other way of creating a spring project which is not in STS IDE is:-

1. Open Spring.start.io

2. Spring Initializer opens.

3. Select the options and add dependencies and artifacts etc>Generate project

4. A Zip file is downloaded. Unzip it and open it with any IDE and work normally.

Coming back to our STS project, we need to make a class that can receive the data coming in and going out of our application.

1.under src/main/java, right click>new package>java class

The data which we are receiving from the client is in the form of HTTP requests like GET, PUT, etc. The Development of an application mainly depends upon the way in which the developer handles the incoming HTTP request. From HTTP GET to PUT, each request requires a specific method so that some processing can be done when the user uses the above requests.

  1. In the above-created class, add @RestController. This annotation registers the class as a controller and it’ll help to receive and match HTTP requests and the URL path. Basically, this is the class where all the data comes to.

2. Below the above annotation add @RequestMapping (“path”). It’s used to map web requests to specific handler classes or handler methods. The parameter specifies the path (or simply URL)through which the client needs to call the Spring application.

3. Inside the above class, we need to create methods that will process info from the user and send the processed information back. There are 4 main types of HTTP requests through which the data can come and go out and we need a method for each of them.

#1:- getUser()- This method is used to get a particular information form the user. In order to make it respond to HTTP GET request, we need to bind it to the incoming request. As such we use the annotation, @GetMapping. Currently, we just return a string from it to see if it works or not. So return, “getUser method called”.

#2 · createUser()- this method is used to handle an HTTP POST request. It’s used to input data to the database. But currently, we just return some String value to make it work. So, return “createUser method is called”. Annotate it with @PostMapping.

#3· updateUser()- this method used to handle HTTP PUT requests which are used to update any values in the database. But currently, we just return some String value to make it work. So, return “updateUser method is called”. Annotate it with @PutMapping.

#4· deleteUser()- used to handle HTTP DELETE requests and is used to delete values from the database. But currently, we just return some String values to make it work. So, return “DeleteUser method is called”. Annotate it with @DeleteMapping.

NOTE- we need to simultaneously import some packages as all the annotations belong to different sub-packages.

Code with the methods and annotations

This completes all the coding part of our first application. In order to run our application, we just need to click on the run button but as a first time user, we need to initialize/make our configurations based on which our Spring app will run. As such:-

1. Scroll down from the run(green colored) button. Select run configurations.

2. A menu will appear. Select Spring boot app from the leftmost column and double click on it. A right panel will open.

3. Select Spring boot from it. Enter the name of your project(demo here) and search for your base package under Main type (if we don’t make any changes to our name in the beginning, then it’ll be given by default). Give a name to your configuration.

4. Click on APPLY and select Run. The Spring app will start running. The console will display the ports and other info.

NOTE- We don’t need to install Tomcat Manually like we used to do in JAVA EE. Spring automatically takes care of this.

Now, the question is that we haven’t made any front-end website or an app, so who is supposed to make HTTP requests? We don’t have a client in place either. In order to fulfill our need of a client, we’ll be using POSTMAN, a software which works as a fake client and sends HTTP requests on behalf of a user.

Download the POSTMAN app from chrome apps store.

  1. OPEN the app.
  2. A URL bar will be visible on the screen.
  3. Enter the URL http://localhost:8080/path. Here the path is the name which we have given inside @RequestMapping (“path”). The in-built Tomcat of the Spring will respond to the 8080 localhost port.
  4. Select any type of request from the drop-down menu beside the URL bar in the Postman app and click on send button. An output will be displayed on the screen which is the same as the String which we have returned from the selected method.
The Postman Screen

Keep Changing the HTTP methods from POST to DELETE and keep observing the change. The actual applications work in the same way. We just need to replace the Strings inside the methods to actual Database codes and we are good to go. APIs such as REST, SOAP, etc work in the exact same way with some minor changes in place. To learn more about APIs, check out the link:-

https://medium.com/@nikhilbhatnagar/application-programming-interface-api-d8582ea91fb3

Congratulations, on your first Spring application. Hope you enjoyed the article! Thank you.

--

--

Nikhil Bhatnagar

A tech enthusiast who loves to read and write about new technologies and trends. Software engineer @HashedinByDeloitte