chevron_left
70 points
7 3 1

REST APIs / Services with Spring boot

 
First create a project with spring starter from here :- http://start.spring.io , Fill the form and select dependencies from add dependencies button. Select lombok, Spring Data JPA, H2 Database.
 
Then Generate a project and open it in your IDE.
 
Now we will need below things minimum to develop REST API :
 
Controller – Which will serve to HTTP end points.
Service – Which will be used for defining our business logic.
Repository – Which will be used for communicating with DB with our POJO structure.
Model – Which will be our model which directly communicate to DB and HTTP call.
 
Now let’s first make Our Model ---
 
@Entity
@Data
@Table(name="user")
public class UserDTO {

@Id
@GeneratedValue(strategy=Identity.All)
private Long id;

private String name;

private String mobile;

private String address;

private Integer age;

}

 

Now let’s make Repository layer ---
 
public interface HomeRepository extends JpaRepository<UserDTO,Long>{
}
 

 

Now We will need to make our service which will used to get users.
 

@Service

public class HomeService{


@Autowire

private HomeRepository homeRepo;


public List<UserDTO> getAll() {

return homeRepo.findAll();

}

}

 

Create a controller like this and import inbuilt classes …...

 

​

@RestController
public class HomeController{

@Autowire
private HomeService homeService;

@GetMapping(“/getUser”)
public List<UserDTO> getUsers(){
return homeService.getAll();
}

}
​

 

 

Now we will need to configure  H2 properties in proprty file ---

 

spring.datasource.url=jdbc:h2:mem:usersapi


Now in post man hit api with http://localhost:8080/getUser  this will get All users  in database. In nnext blog we will see full CRUD with Spring boot.

You can also visit other Spring boot blogs of mine from below links :- 

Bean Life cycle – https://kodlogs.com/blog/9239/spring-bean-life-cycle-and-creation-of-bean-with-bean-scope

Various Annotations – https://kodlogs.com/blog/9763/framework-annotations-%40autowire-%40component-%40configuration

Postgre CRUD – https://kodlogs.com/blog/9348/create-web-application-in-spring-boot-with-postgresql-part


2 Comments

I&rsquo;m so happy to meet this blog that makes me understand a lot. After I read it again and again, I understand many lessons in this blog.
Thank you. A thousand thousand blessings to you for sharing knowledge.

More Posts

Create a Web application in Spring boot with PostgreSQL (Part-3) darshan1507 - May 25
Spring framework annotations @Autowire @Component @Bean @Configuration @Value darshan1507 - Jun 5
Uploading files to AWS S3 Bucket using Spring Boot darshan1507 - Jun 7
Application Properties in Spring Boot thistimeonly32 - Apr 4
Spring AOP Interview Questions Lalit Kumar - Dec 30, 2020
web server failed to start. port 8080 was already in use. SharadMagar450 - Aug 31
Create a Web application in Spring boot with PostgreSQL (Part-2) darshan1507 - May 25
Create a Web application in Spring boot with PostgreSQL (Part-1) darshan1507 - May 25
4 reasons why you should use GraphQL over REST APIs blessinghirwa - Aug 8
How call REST Services in Android Application darshan1507 - Jun 7