Spring Boot || Tutorial 4 || How to handle POST method using Spring Boot, Update in List - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Sunday, 10 June 2018

Spring Boot || Tutorial 4 || How to handle POST method using Spring Boot, Update in List

Today we will see how to handle POST method in Spring Boot. Basically in continuation to the last tutorial - we'll add update operation to the employee list. Let's follow the bellow steps-

(Please Follow us : Liked our post? Follow us https://www.blogger.com/follow.g?view=FOLLOW&blogID=4704532048503018855) 

(Please Donate Us :  https://imjo.in/t9cEFy)


Step-1: Update Controller class with POST request tagged method - addEmployee(). Use the below code for reference

package com.ncteam.thirdtutorial;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class ReturnObjectFromController {

@Autowired
private ReturnObjectService service ;

@RequestMapping("/listofpojo")
public List<ListPojo> listAllEmployee() {

return  service.getEmployeeList();
}
@RequestMapping("/listofpojo/{id}")
public ListPojo listEmployee(@PathVariable String id) {

return service.getEmployee(id);
}

@RequestMapping(method=RequestMethod.POST, value="/addempl")
public void addEmployee(@RequestBody ListPojo listpojo ) {
service.addEmployee(listpojo);
}
}

Step-2: Update the Service layer class with addEmployee() method extension. Use the below code for reference -

package com.ncteam.thirdtutorial;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class ReturnObjectService {

public List<ListPojo> listEmployee = new ArrayList<>(Arrays.asList( new ListPojo("Emp1","Kennith", "NJ"),
new ListPojo("Emp2","Jorge", "NC"),
new ListPojo("Emp1","Melanie", "India")));
public List<ListPojo> getEmployeeList(){
return listEmployee;
}
public ListPojo getEmployee(String emplId) {
return listEmployee.stream().filter(t->t.getEmplId().equals(emplId)).findFirst().get();
}

public void addEmployee(ListPojo listpojo) {
// TODO Auto-generated method stub
listEmployee.add(listpojo);
}
}

Step 3: We are all good lets build the project and test the POST request on Postmaster application (chrome plugin)

Add the EE information as shown in the below image - select Body then raw check box and paste the EE info -


Also don't forget to update Header type to define the input type as JSON 


Click on the Send button - Status 200 OK


If you find any difficulty to use Postman please let us know if below comment box.

Please donate us -> https://imjo.in/t9cEFy

1 comment:

  1. Liked our post? Follow us https://www.blogger.com/follow.g?view=FOLLOW&blogID=4704532048503018855

    ReplyDelete

Post Bottom Ad