0% found this document useful (0 votes)
38 views

Spring Boot REST CRUD Application With POSTMAN Client

Uploaded by

amancsc2212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Spring Boot REST CRUD Application With POSTMAN Client

Uploaded by

amancsc2212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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

GET – Read Operation (Select Command)

POST – Write / Create Operation (Insert Command)

PUT – Update Operation (Update Command)

DELETE – Delete Operation (Delete Command)

- Create table “ticket” in MySQL


mysql>create table ticket (ticket_id int(3),passenger_name varchar(10),
source_station varchar(15), dest_station varchar(15), email varchar(20));

- Create a Spring Starter Project in STS


Name : SpringBootRESTCRUDProj
Type : Maven Project
Java Version: 8/17/21
Group : com.rest.springboot
Artifact: RestSpringBootCRUDProj
Package : com.rest.springboot
Click Next

Add the following project dependencies


o Spring Web
o Spring Data JPA
o MySQL Driver
o Lombok
Click Finish

- Create a package “com.rest.springboot.entities” in src/main/java folder


- Create a Java Bean class “Ticket” in “com.rest.springboot.entities”
package

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

public interface TicketBookingRepository extends JpaRepository<Ticket,


Integer>{

- Create a package “com.rest.springboot.service” in src/main/java folder


- Create a Service class “TicketBookingService” in com.rest.springboot.service
package

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;

public Ticket createTicket(Ticket ticket) {


return ticketBookingRepository.save(ticket);
}
public Ticket getTicketById(Integer ticketId) {
return ticketBookingRepository.findById(ticketId).get();
}
public List<Ticket> getAllBookedTickets() {
return ticketBookingRepository.findAll();
}
public void deleteTicket(Integer ticketId) {
ticketBookingRepository.deleteById(ticketId);
}
public Ticket updateTicket(Integer ticketId, String newEmail)
{
Ticket ticketFromDb =
ticketBookingRepository.findById(ticketId).get();
ticketFromDb.setEmail(newEmail);
Ticket upadedTicket = ticketBookingRepository.save(ticketFromDb);
return upadedTicket;
}
}

- Create a package “com.rest.springboot.controller” in src/main/java


folder
- Create a Rest Controller class “TicketBookingController” in
com.rest.springboot.controller package

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}")

public Ticket getTicketById(@PathVariable("ticketId")Integer


ticketId){
return ticketBookingService.getTicketById(ticketId);
}
@GetMapping(value="/ticket/alltickets")
public List<Ticket> getAllBookedTickets(){
return ticketBookingService.getAllBookedTickets();
}

@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

- Run the project


Right click on RestSpringBootCRUDProj -> Run As -> Spring Boot App
Test the application using POSTMAN
Open POSTMAN and do the following requests

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":"[email protected]"}

Click Send

Add some more records


{"ticketId":"2", "passengerName":"Ramu","sourceStation":"Chennai","destStation":"Mu
mbai","email":"[email protected]"}

{"ticketId":"3", "passengerName":"Ramya","sourceStation":"Pune","destStation":"Jaip
ur","email":"[email protected]"}

GET by Id Request
GET http://localhost:8080/api/tickets/ticket/1

GET http://localhost:8080/api/tickets/ticket/2

GET All Tickets Request

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/[email protected]

You might also like