Produces is a media type.
-> It represents the response formats supported by REST Controller Method.
-> One method can support multiple response formats (xml and json).
produces = { "application/xml", "application/json" }
-> Client should send a request with "Accept" http header.
-> Accept header represents in which format the client expects a response from the REST API.
-> Based on the Accept header value 'Message Converter' will convert the response into client's expected format.
@Data
@XmlRootElement
@NoArgsConstructor
@AllArgsConstructor
public class Product {
private Integer pid;
private String pname;
private Double price;
}
@RestController
public class ProductRestController {
@GetMapping(
value = "/product",
produces = { "application/xml", "application/json" }
)
public ResponseEntity<Product> getProduct() {
Product p1 = new Product(101, "Monitor", 8000.00);
return new ResponseEntity<>(p1, HttpStatus.OK);
}
@GetMapping("/products")
public ResponseEntity<List<Product>> getProducts(){
Product p1 = new Product(101, "Monitor", 8000.00);
Product p2 = new Product(102, "RAM", 6000.00);
Product p3 = new Product(103, "CPU", 15000.00);
List<Product> products = Arrays.asList(p1,p2,p3);
return new ResponseEntity<>(products, HttpStatus.OK);
}
}
Working with HTTP POST Request
############################
-> HTTP POST request is used to create new resource/record at server.
-> POST request contains request body.
-> Client can send data to server in Request Body.
-> To bind Rest Controller method to POST request we willl use @PostMapping.
->To read data from Requet body we will use @RequestBody annotation.
-> "consumes" represents in which formats method can take input.
-> "Content-Type" header represents in which format client sending data in request body.
@Data
@XmlRootElement
public class Book {
private Integer id;
private String name;
private Double price;
}
---------------------------
@RestController
public class BookRestController {
@PostMapping(
value = "/book",
consumes = { "application/json", "application/xml" }
)
public ResponseEntity<String> addBook(@RequestBody Book book) {
System.out.println(book);
// logic to store in DB
String msg = "Book Added Succesfully";
return new ResponseEntity<String>(msg, HttpStatus.CREATED);
}
}
----------------
{
"id" : 101,
"name" : "Java",
"price" : 450.00
}
-> produces vs consumes
-> Content-Type vs Accept
-> produces attribute represents in which formats Method can provide response data to clients.
-> consumes attribute represents in which formats Method can take request data from clients.
-> Accept header represents in which format client expecting response from REST API.
-> Content-Type header represents in which format client is sending request data to REST API.
Note: We can use both Consumes & Produces in single REST API method.
++++++++++++++++++++++++++++++++++++++++++++
Requirement : Develop IRCTC REST API to book a train ticket
++++++++++++++++++++++++++++++++++++++++++++
-> To develop any REST API first we have to understand the requirement.
-> Identify input / request data
-> Identify output / response data
-> Create request & response binding classes
-> Create REST Controller with required methods.
-> Test REST API methods behaviour using Postman
@Data
public class PassengerInfo {
private String name;
private Long phno;
private String jdate;
private String from;
private String to;
private Integer trainNum;
}
@Data
public class TicketInfo {
private Integer ticketId;
private String pnr;
private String ticketStatus;
}
@RestController
public class TicketRestController {
@PostMapping(
value = "/ticket",
produces = {"application/json"},
consumes = {"application/json"}
)
public ResponseEntity<TicketInfo> bookTicket(@RequestBody PassengerInfo request){
System.out.println(request);
//logic to book ticket
TicketInfo tinfo = new TicketInfo();
tinfo.setTicketId(1234);
tinfo.setPnr("JLJL6868");
tinfo.setTicketStatus("CONFIRMED");
return new ResponseEntity<>(tinfo, HttpStatus.CREATED);
}
}
-------------------------------
{
"name" : "Ashok",
"phno" : 12345678,
"jdate" : "05-08-2022",
"from" : "hyd",
"to" : "pune",
"trainNum" : 8574
}
{
"ticketId": 1234,
"pnr": "JLJL6868",
"ticketStatus": "CONFIRMED"
}
##############
HTTP PUT Request
##############
-> PUT request is used to update an existing record / resource at server.
-> PUT Request can take data in URL and in Request Body.
-> To bind our method to PUT request we will use @PutMapping.
@PutMapping("/ticket")
public ResponseEntity<String> updateTicket(@RequestBody PassengerInfo request){
System.out.println(request);
//logic to update ticket
return new ResponseEntity<>("Ticket Updated", HttpStatus.OK);
}
##################
HTTP DELETE Request
##################
-> DELETE request is used to delete an existing record / resource at server.
-> DELETE Request can take data in URL and in Request Body.
-> To bind our method to DELETE request we will use @DeleteMapping.
@DeleteMapping("/ticket/{ticketId}")
public ResponseEntity<String> deleteTicket(@PathVariable("ticketId") Integer ticketId){
//logic to delete the ticket
return new ResponseEntity<>("Ticket Deleted", HttpStatus.OK);
}
------------------------------------------
Q) Can we write the logic to update a record in POST request method ?
Ans) Yes, we can do it but not recommended. We need to follow HTTP Protocol standards while developing REST API.
Comments
Post a Comment