Saturday, 17 February 2018

Spring Boot Restful Web Services CRUD Example GET & POST | Tutorial - 2

Spring Boot Restful Web Services CRUD Example GET & POST



Welcome to spring boot restful web services crud example tutorial.

In this spring boot restful web services crud example tutorial we will learn following:

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

2. As part of spring boot restful web services crud example we will learn implementation of GET & POST methods.

In previous spring boot restful web services crud example tutorial we have learned following:

1. How to create spring boot restful web services example project using spring tool suite.

2. How to implement a basic spring boot restful web services crud example for sample Account spring boot restful web service.

In this spring boot restful web services crud example tutorial we will learn about GET & POST method implementation. And also about different spring boot annotations.

Content of this spring boot restful web services crud example tutorial is are as follows:

1. Implementing a spring boot restful web services crud example domain
2. Implementing a spring boot restful web services crud example service
3. Implementing a spring boot restful web services crud example controller
4. CURL commands for GET & POST

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:
Implementing a spring boot restful web services crud example domain

package com.sjtech.domain;
public class Account {
String name;
Long id;
String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
view raw Account.java hosted with ❤ by GitHub
Implementing a spring boot restful web services crud example service

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);
}
}
Implementing a spring boot restful web services crud example 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);
}
}
CURL commands for GET & POST

--POST:
curl -H "Content-Type: application/json" -X POST -d '{"id":1, "name": "account 1", "email":"account@test.com"}' http://localhost:8080/accounts/
--GET:
curl http://localhost:8080/accounts/
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...