Create your first API with Java and Spring

image

Spring is a Java framework that aims to facilitate the construction and upload of API's (Application Programming Interfaces) and web applications. It is widely accepted and used in the market, being considered as an important tool in the arsenal of a Java developer. This article seeks to introduce the framework and help you create your first application using this tool.

But before we continue, a quick thought

The framework of a programming language aims to abstract complexities and facilitate its use, within a clear and directed purpose. In the case of Spring, it has a series of configurations and libraries that make tasks a lot easier, such as publishing the back-end of the web application, creating RESTful APIs, integrating with the database, making Java less "verbose" in general , among many other points.

However, it is worth mentioning that learning a framework does not exempt us from studying and knowing the language itself. We need to build a solid foundation in case we run into problems in the application and need to "open the hood of the car" to perform the analysis and resolution. Furthermore, without concepts of programming logic, data structure, OOP and Java, we run the risk of becoming "hostages" of Spring. This can greatly limit our opportunities and scope of action, making it difficult to fully understand what the framework and application are doing.

However, it's okay if you've never programmed in Java and want to start here, but it's nice to use this article as a "kick start" for other studies about the language.

Using spring initializr to create the project

spring initializr is a web application that allows the creation of a Spring project with its structure ready according to the selected libraries and configurations. It is worth mentioning that initializr does not code the project, its objective is to provide its structure and "chassis".

To use initializr, just access https://start.spring.io/ . Once the project creation screen appears, you can keep the latest Spring version selected (as of this writing, the version is 2.5.5, do not select SNAPSHOT versions). Then click on ADD DEPENDENCIES , search for Web and select Spring Web . Validate that the settings are as shown in the image below and click on GENERAT

image

Creating our first code

After following the procedures of the previous step, unzip the generated folder and open it in the IDE or code editor of your choice (if you don't know any, try IntelliJ ). Select the DemoApplication.java class , located in src/main/java/com/example/demo and edit the code as follows:

                        1package com.example.demo;
                            2
                            3import org.springframework.boot.SpringApplication;
                            4import org.springframework.boot.autoconfigure.SpringBootApplication;
                            5import org.springframework.web.bind.annotation.GetMapping;
                            6import org.springframework.web.bind.annotation.RestController;
                            7
                            8@SpringBootApplication
                            9@RestController
                            10public class DemoApplication {
                            11
                            12    public static void main(String[] args) {
                            13        SpringApplication.run(DemoApplication.class, args);
                            14    }
                            15
                            16    @GetMapping("/lets-code")
                            17    public String letsCode() {
                            18        return String.format("Let's Code!");
                            19    }
                            20}

                    

In short, the class marked with @SpringBootApplication is the class responsible for uploading the application, leaving it available for consumption. Furthermore, we are using the @RestCrontroller annotation to indicate that this class will also represent the layer that receives and controls requests from a web consumer.

The @GetMapping marked in the method called letsCode represents that we are going to query information that will be provided by the domain that we named lets-code . The expected return of this application's call is a String with the content "Lets Code!".

Uploading and running the application

To run the application in your IDE, just click on the "Run" button, the same one used to run pure Java applications. If the upload is successful the console will display some outputs about the publication, like:

image

By default, the application is available for calling on port 8080 of your computer, so to test the application we must type http://localhost:8080/lets-code . Note that we are using the "address" /lets-code , provided in our @GetMapping .

As soon as we access the address in our browser, the following message should appear:

"I made my first code in Spring. Now what?"

Now that your first code has been created and you have a foundation in the power of Spring, try accessing the official framework documentation . Also, look for some concepts about RESTful API's. In addition, if you do not yet have the basis mentioned throughout the article, it would be very interesting for you to obtain knowledge on the topics mentioned!