With new emerging technologies, companies must quickly adapt and upgrade to more efficient ones. Spring Boot is an open-source Java web framework that allows developers to get started with an auto configurable production-grade Spring application. In this blog, you will go through the process of creating a simple Spring Boot application with a Database for storing the information. You will learn how to create HTTP endpoints and expose them for storing and retrieving data from the Database in JSON format.
The steps to create an application with Spring Boot are listed below:
The Spring Initializr will provide all the necessary configurations for the project. Then, you need to code the Data class for managing messages and add Database support to it. Finally, you expose the endpoints of the API.
data class Message(val id: String?, val text: String)
@RestController
class MessageResource {
@GetMapping
fun index(): List<Message> = listOf(
Message(“1”, “First”),
Message(“2”, “Second”),
Message(“3”, “Third”),
)
}
package demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.annotation.Id
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@RestController
class MessageResource {
@GetMapping
fun index(): List<Message> = listOf(
Message(“1”, “First”),
Message(“2”, “Second”),
Message(“3”, “Third”),
)
}
data class Message(val id: String?, val text: String)
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
@Table(“messages”)
data class Message(@Id val id: String?, val text: String)
import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.CrudRepository
interface MessageRepository : CrudRepository<Message, String>{
@Query(“select * from messages”)
fun findMessages(): List<Message>
}
Select * from messages
import org.springframework.stereotype.Service
@Service
class MessageService(val db: MessageRepository) {
fun findMessages(): List<Message> = db.findMessages()
fun post(message: Message){
db.save(message)
}
}
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.PostMapping
@RestController
class MessageResource(val service: MessageService) {
@GetMapping
fun index(): List<Message> = service.findMessages()
@PostMapping
fun post(@RequestBody message: Message) {
service.post(message)
}
}
CREATE TABLE IF NOT EXISTS messages (
id VARCHAR(60) DEFAULT RANDOM_UUID() PRIMARY KEY,
text VARCHAR NOT NULL
);
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./data/testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.schema=classpath:sql/schema.sql
spring.datasource.initialization-mode=always
POST http://localhost:8080/
Content-Type: application/json
{
“text”: “First”
}
POST http://localhost:8080/
Content-Type: application/json
{
“text”: “Second”
}
POST http://localhost:8080/
Content-Type: application/json
{
“text”: “Third”
}
GET http://localhost:8080/
In this post, you learnt how to create a simple Spring Boot application with an HTTP endpoint, create a Database for storing objects, returning data in JSON format, and about endpoints for writing and retrieving Database objects.
Data will continue to play an outsized role for marketers in 2023. These changes are,… Read More
With the integration of Match-Trader, B2Broker, a global liquidity and technology provider, expands its white… Read More
The future of higher education is here! It's hard to imagine what higher education in… Read More
As the world continues to recover from the pandemic, the field of software development is… Read More
Introducing colocation Colocation is an increasingly popular option for businesses looking to host their own… Read More
ANPR, or Automatic Number Plate Recognition, is the technology employed to gather location information by… Read More