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:
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
Implementing a spring boot restful web services crud example service
Implementing a spring boot restful web services crud example controller
CURL commands for GET & POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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/ |
No comments:
Post a Comment