Today we are going to learn create RESTfull APIs using Spring Boot :
A) Create Model class:
- Vary first we have to create a model class and some field which have mapped with table in database.
-
123456789101112import lombok.*;import javax.persistence.*;@Entity@Table@Datapublic class CompanyProfile {@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;private String companyName;private String companyAddress;}
- @Entity : This annotation used for the identify the model class.
- @Table : this is used for the put the name of table in database.
- @Data: This annotation has used for the create Getter, Setter, Constructor, to String methods and for this feature have to add Lombok dependency in pom.xml.
- @Id: it is used for make the id in table.
- @GenatedValue: It is used for the crate the Id auto increment.
B) Create Repository interface:
- Now we will create a interface which is CompanyProfile for the mapping the connect to the database . it will help to manipulate the data in database.
- In the interface we use JPARepository which is provided by the JPA. it has all database manipulation predefined methods.
- In this method we will pass CompanyProfile which is model class and Id which is in Integer.
1 2 3 4 |
@Repository public interface CompanyRepo extends JpaRepository<CompanyProfile,Integer> { } |
C) Create service interface:
- We will create the service interface and create the method which will communicate with controller class and service implementation as you can see below.
1 2 3 |
public interface CompanyService { public CompanyProfile createCompany(CompanyProfile companyProfile); } |
D) Create service implementation class:
- In this call we can white his login related to our business and it will implement the service class as well and override the method of service interface which have to create in service interface
- In the service implantation class will mark as @service annotation which can well to identify of service class.
- We can @autowire the repository as well which will provide the all predefine JPA method to manipulation of database as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.dev.model.CompanyProfile; import com.dev.service.CompanyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dev.repository.CompanyRepo; @Service public class CompanyProfileImpl implements CompanyService { @Autowired private CompanyRepo companyRepo; @Override public CompanyProfile createCompany(CompanyProfile companyProfile) { return this.companyRepo.save(companyProfile); } } |
E)Create Controller class:
- And finally, we will create the controller class which will responsible to get the and send the HTTP and HTTPS methods.
- In the Controller class we have to mention as @RestController (@Controller + @ResponseBody) which is the responsible for get the JSON data.
- Firstly, have to create the method createCompany and @autowire the CompanyService and with the help of companyService we will call createCompany method which have already created in service class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.dev.model.CompanyProfile; import com.dev.service.CompanyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class CompanyProfileController { @Autowired private CompanyService companyService; @PostMapping("/profile") public CompanyProfile createCompany(@RequestBody CompanyProfile companyProfile) { return this.companyService.createCompany(companyProfile); } } |
Postman request:
bluethinkinc_blog
2023-09-14