Are you thinking of working on a Spring Boot Web Project? Not to worry as we will help you get through via simple steps.
Spring boot is a Java Framework, which is used to develop enterprise-grade applications. It provides advanced configuration options and plugin extensions for further customization. In this post, we will help you to expose a web Application Programming Interface (API) using a spring boot application.
To begin with, visit the Spring boot initializer tool and select spring web in the dependencies section while keeping other configurations as default.
Click on the generate button. once done, unzip the downloaded file, and import in Eclipse IDE as a maven project.
Open the pom XML file and you will notice that the maven dependencies are added automatically. A maven dependency should have a version property, but in this pom, no versions are mentioned as the application will inherit dependency versions from the parent pom.
Right click on the DemoApplication.java file and run as a java application. Spring boot comes with an embedded tomcat server, so a standard run should start a localhost server on port 8080. Default port can be modified using the “server.port” property in the application.properties file, for example, server.port=8088.
This is a plain app running on your local server with no functionality. In the following steps, we will add a simple web endpoint.
Create a new class named DemoController.java, this will expose a /demo web endpoint on your localhost server. In general, web project classes can be categorized into three categories i.e. Controller, Service, and Repository.
Controllers are the entry point of your application, exposing URL endpoints and defining expected request and response content types (JSON, XML, etc.) and other related configurations.
Service classes are used to write the business logic of your application, like if you need to save your request data in a file based on some rules, then the logic to do that should be written in service classes.
A repository is the data access layer, used to write code to interact with databases or any kind of data store.
For this example, we will create only a controller class to test our endpoint, required functionality can then be added based on requirements.
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/demo")
public String demoFunction() {
return "hello world\n";
}
}
Now again run the app as a java application, a localhost server should start exposing the new endpoint. Once the server startup is complete, go to the browser and open http://localhost:8080/demo URL.
We can also use a command-line utility called “curl” to test our new endpoint, run the curl command like shown below on a Unix prompt or windows dos prompt.
The curl command is generally available across multiple operating systems but if not available by default, then you can install curl as well. You can visit curl’s official website for more details on command usage and installation steps.
curl http://localhost:8080/demo
By default, the exposed endpoints are not secured, anyone can access the URL if deployed on a public server. To enable basic security, we can add the spring boot starter security dependency as shown below.
Run the app again as a java application after adding the security dependency, you should see a security password automatically generated and printed in startup logs for a default “user” account.
Now try to access the http://localhost:8080/demo URL again and you should see a login page.
Login with the password from startup logs and you should see the “hello world” message after successful login.
We can change the default user and password using the application properties file if required. By specifying these properties, we are asking spring boot security auto-configuration to not generate a default password.
Printing passwords in log or console output can be a high-security risk vulnerability in your app.
We can also use curl to access a secure endpoint by providing the user and password, have provided an example below.
curl –user appuser:appuser123 http://localhost:8080/demo
Through this post, we learned how to setup a basic spring boot project. To learn more about it, you can access the spring boot documentation for more details and available configurations. The sample application discussed in this post is available on GitHub for your quick reference related to the actual code.
If you have any questions or feedback, please share in the comments section.