Tuesday, 20 February 2018

Spring boot CRUD Operations example PUT & DELETE | Tutorial - 3

Spring boot CRUD Operations example 
PUT & DELETE



Welcome to spring boot restful web service tutorial, Tutorial -3 Spring boot CRUD Operations example PUT & DELETE.

In today's spring boot crud operations example we will learn following:

1. How to implement spring boot crud operations for sample Accounts spring boot restful web service.

2. As part of spring boot crud operations example tutorial we will learn implementation of PUT & DELETE methods.

3. We will also cover some corner use cases as part of this spring boot crud operations example tutorial.

In previous spring boot restful web service tutorial we have learned following:

1. How to implement spring boot crud operations example for sample Account spring boot restful web service.

2. In that spring boot crud operations example tutorial we have learn implementation of GET & POST method.

Content of this spring boot crud operations example tutorial is are as follows:

1. Implementation of spring boot service methods for PUT & DELETE methods
2. Implementation of spring boot crud operations methods in spring boot restful controller
3. Handling some corner use cases in spring boot crud operations example
4. Demo using CURL commands

spring boot CRUD operations means implementing CRUD operations using spring boot. Spring boot CRUD operations involves different operations such as create, read, update & delete. With respect to this different spring boot crud operations there are different HTTP methods such as POST, GET, PUT & DELETE.

For more details please check out following videos:

Implementation of spring boot service methods for PUT & DELETE methods
 
package com.sjtech.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.sjtech.domain.Account;
@Service
public class AccountService {
Map<Long, Account> database = new HashMap<>();
public Account findById(Long id) {
return database.get(id);
}
public List<Account> getAll(){
return new ArrayList<>(database.values());
}
public void create(Account account) {
database.put(account.getId(), account);
}
public void update (Account updatedAccount) {
Account accountToBeUpdated = database.get(updatedAccount.getId());
accountToBeUpdated.setName(updatedAccount.getName());
accountToBeUpdated.setEmail(updatedAccount.getEmail());
}
public void delete (Long id) {
database.remove(id);
}
}
Implementation of spring boot crud operations methods in spring boot restful controller
 
package com.sjtech.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
import com.sjtech.domain.Account;
import com.sjtech.service.AccountService;
@RestController
public class AccountController {
@Autowired
AccountService service;
@RequestMapping(value="/accounts/{id}",method=RequestMethod.GET)
public ResponseEntity<Account> getAccountById(@PathVariable Long id){
return new ResponseEntity<Account>(service.findById(id), HttpStatus.OK);
}
@RequestMapping(value="/accounts/",method=RequestMethod.GET)
public ResponseEntity<List<Account>> getAcounts(){
return new ResponseEntity<List<Account>>(service.getAll(), HttpStatus.OK);
}
@RequestMapping(value="/accounts/",method=RequestMethod.POST)
public ResponseEntity<String> createAccount(@RequestBody Account account){
service.create(account);
return new ResponseEntity<String>("Account Created Successfully", HttpStatus.CREATED);
}
@RequestMapping(value="/accounts/{id}",method=RequestMethod.PUT)
public ResponseEntity<String> updatedAccount (@PathVariable Long id, @RequestBody Account updatedAccount) {
if (service.findById(id) == null) {
return new ResponseEntity<String>("Account not found", HttpStatus.NOT_FOUND);
} else {
service.update(updatedAccount);
return new ResponseEntity<String>("Account updated successfully", HttpStatus.OK);
}
}
@RequestMapping(value="/accounts/{id}",method=RequestMethod.DELETE)
public ResponseEntity<String> deleteAccount (@PathVariable Long id) {
if (service.findById(id) == null) {
return new ResponseEntity<String>("Account not found", HttpStatus.NOT_FOUND);
} else {
service.delete(id);
return new ResponseEntity<String>("Account deleted successfully", HttpStatus.OK);
}
}
}
CURL Commands
 
-- PUT
curl -H "Content-Type: application/json" -X PUT -d '{"id":1, "name": "account updated", "email":"account@test.com"}' http://localhost:8080/accounts/1
-- DELETE
curl -X DELETE http://localhost:8080/accounts/1
view raw script.sh hosted with ❤ by GitHub

No comments:

Post a Comment

Spring Boot JPA

Spring Boot JPA  In 5 Simple Steps Integrate PostgreSQL Database | Tuorial - 4 Hello Friends, Welcome to Spring Boot JPA tutorial...