Working RESTful API in Spring Boot
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
RESTful APIs have been all over the web development these days. RESTful API enables easy communication between systems using JSON/XML over age old HTTP protocols. This makes them easy and familiar to use with minimum learning curve.
With the knowledge of Basic annotation let’s make a working API . Here we will use H2 embedded database for our use and will try to perform CRUD using RESTful operations.
Let us start by configuring dataSource . As we know Spring boot has an opinionated approach , it does provide simple application properties to create a dataSource.
|
# H2 spring.h2.console.enabled=true spring.h2.console.path=/h2 # Datasource spring.datasource.url=jdbc:h2:file:~/user spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver |
The first two properties are used for enabling H2 console. Since it is a embedded database these property allow us to use a UI to see the database and run queries. When system will start it will ensure that we can see the database at http://localhost:port_number/h2
After that we are using few default properties for creating data source. These are pretty standard properties to crete any dataSource i.e. database URL ,username ,password and driver. Here we are using H2 related configuration.
Let’s start with creating an API for creating,updating,retrieving and deleting a User for an Application. We will keep things simple and below is our domain model for User
@Entity
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String email;
public User() {
super();
}
//getters and setters
}
We are using few annotation from JPA to use it for inserting in database.
@Entity : Tells there should be a table for User object.
@Id : denotes that the field will be used as a primary key.
@GeneratedValue: Let’s decide how that primary key will be generated , in our case it is GenerationType.AUTO which means it will be automatically generated with increments.
Now once we have a full User object and Entity , let’s see how our easily and magically we will be adding a way to operate database related operation.
The Repositories
We will be creating a simple repository which will help us to talk to database and do User related operation. Using JPA we can create an interface say UserRepository and that will help us to perform CRUD operations over User table.
package com.codingsaint.tutorial.spring.userservice.repository;
import com.codingsaint.tutorial.spring.userservice.model.User;
@Repository
public interface UserRepository extends
JpaRepository<User,Long> {
}
If you see above interface we annotated it with @Repository to make it a Spring bean and also to demote that this bean does an database related operation. Further we extented it to JPARepository enabling it to do CRUD operations. Now JPARepository has methods like save, get, getAll, delete which will be available to UserRepository by default.
Let’s autowire it to a Controller and handle incoming HTTP requests.
Now we will create a controller which will help us to communicate with clients . Let’s start with saving an object.Below is sample Rest Controller we are using.
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping(path = { "/user" }, method = RequestMethod.POST)
public ResponseEntity<Void>add(@RequestBody User user) {
ResponseEntity<Void> response = null;
try {
userRepository.save(user);
response = new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
response = new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
return response;
}
}