How to Create Rest API using Spring Boot


Step 1:  Add given dependency in pom.xml

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.33</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
</dependency>

Step 2: Create new class UserController.java file and copy & paste given code.

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dto.User;
import com.example.demo.service.UserService;

@RestController
@RequestMapping("api/v1/")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	// Get all users 
	@GetMapping("/users") 
	public List getAllUsers() { 
		return userService.getAllUsers(); 
	}
}

Step 3: Create new Class User.java file and copy & paste given code.


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
	private Long id;
	private String name;
	private String username;
	private String password;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

Step 4: Create interface UserService.java file and copy & paste given code.

import java.util.List;
import com.example.demo.dto.User;

public interface UserService {

	List getAllUsers();
}

Step 5: Create class UserServiceImpl.java file and copy & paste given code.


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dto.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserRepository userRepository;

	@Override
	public List getAllUsers() {
		return userRepository.findAll();
	}

}

Step 6: Create Interface UserRepository.java file and copy & paste given code.

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.dto.User;

@Repository
public interface UserRepository extends JpaRepository<User,Long> {

	  List findAll();
}

Step 7: Go to Application properties file and add given code.

spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Now user API is ready http://localhost:8080/api/v1/users
Download Code click here

Step 8: That’s all.