Spring Boot REST CRUD Application With POSTMAN Client
Spring Boot REST CRUD Application With POSTMAN Client
POSTMAN Tool
Postman is an interactive and automatic tool for verifying the APIs of the
project. Postman is a Google Chrome App for interacting with HTTP APIs
Download POSTMAN
url: https://www.postman.com/downloads
HTTP Methods
Ticket.java
package com.rest.springboot.entities;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name="ticket")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Ticket {
@Id
@Column(name="ticket_id")
private Integer ticketId;
@Column(name="passenger_name",nullable=false)
private String passengerName;
@Column(name="source_station")
private String sourceStation;
@Column(name="dest_station")
private String destStation;
@Column(name="email")
private String email;
}
- Create a package “com.rest.springboot.repository” in src/main/java
folder
- Create an interface “TicketBookingRepository” in
com.rest.springboot.repository package
TicketBookingRepository.java
package com.rest.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.rest.springboot.entities.Ticket;
@Repository
TicketBookingService.java
package com.rest.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rest.springboot.repository.TicketBookingRepository;
import com.rest.springboot.entities.Ticket;
import java.util.List;
@Service
public class TicketBookingService {
@Autowired
private TicketBookingRepository ticketBookingRepository;
TicketBookingController.java
package com.rest.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rest.springboot.entities.Ticket;
import com.rest.springboot.service.TicketBookingService;
import java.util.List;
@RestController
@RequestMapping(value="/api/tickets")
public class TicketBookingController {
@Autowired
private TicketBookingService ticketBookingService;
@PostMapping(value="/create")
public Ticket createTicket(@RequestBody Ticket ticket){
return ticketBookingService.createTicket(ticket);
}
@GetMapping(value="/ticket/{ticketId}")
@DeleteMapping(value="/ticket/{ticketId}")
public void deleteTicket(@PathVariable("ticketId")Integer
ticketId){
ticketBookingService.deleteTicket(ticketId);
}
@PutMapping(value="/ticket/{ticketId}/{newEmail:.+}")
public Ticket updateTicket(@PathVariable("ticketId")Integer
ticketId,@PathVariable("newEmail")String newEmail){
return
ticketBookingService.updateTicket(ticketId,newEmail);
}
}
- Update application.properties file of src/main/resources folder
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Create Request
POST http://localhost:8080/api/tickets/create
In Body, Select Raw and Select JSON
{"ticketId":"1", "passengerName":"Raj","sourceStation":"Hyderabad","destStation":"D
elhi","email":"raj@gmail.com"}
Click Send
{"ticketId":"3", "passengerName":"Ramya","sourceStation":"Pune","destStation":"Jaip
ur","email":"ramya@gmail.com"}
GET by Id Request
GET http://localhost:8080/api/tickets/ticket/1
GET http://localhost:8080/api/tickets/ticket/2
GET http://localhost:8080/api/tickets/ticket/alltickets
DELETE by Id Request
DELETE http://localhost:8080/api/tickets/ticket/1
UPDATE Request
PUT http://localhost:8080/api/tickets/ticket/2/ramu_new@gmail.com