[Spring] Java를 이용한 간단한 CRUD API만들기
- ETC../Spring
- 2021. 1. 29.
728x90
API 작성
목적 : 모바일 어플리케이션, 웹 등에서 사용되는 API 제작
IDE : IntelliJ
1. Spring Initializer 로 프로젝트 생성
2. 파일 구성
- Controller
- Api 컨트롤러
- Model
- 데이터 필드 정보
코드
UserProfile
package com.example.demo.model;
public class UserProfile {
private String id;
private String name;
private String phone;
private String address;
public UserProfile(String id, String name, String phone, String address) {
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserProfileController
package com.example.demo.controller;
import com.example.demo.model.UserProfile;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class UserProfileController {
private Map<String, UserProfile> userMap;
@PostConstruct
public void init() {
userMap = new HashMap<String, UserProfile>();
userMap.put("1", new UserProfile("1", "홍길동", "111-1111", "서울시 강남구 대치1동"));
userMap.put("2", new UserProfile("2", "김철수", "111-1112", "서울시 강남구 대치2동"));
userMap.put("3", new UserProfile("3", "김미자", "111-1113", "서울시 강남구 대치3동"));
}
//GetMapping 일반적으 데이터 조회로
//Post 데이터 수정
//Put 데이터 생성
//Delete 데이터 삭제
@GetMapping("/user/{id}")
public UserProfile getUserProfile(@PathVariable("id") String id) {
return userMap.get(id);
}
@GetMapping("/user/all")
public List<UserProfile> getUserProfileList() {
return new ArrayList<UserProfile>(userMap.values());
}
//RequestParam 으로 인자 전달 받는다
@PutMapping("user/{id}")
public void putUserProfile(
@PathVariable("id") String id,
@RequestParam("name") String name,
@RequestParam("phone") String phone,
@RequestParam("address") String address) {
UserProfile userProfile = new UserProfile(id, name, phone, address);
userMap.put(id, userProfile);
}
//수정
@PostMapping("user/{id}")
public void postUserProfile(
@PathVariable("id") String id,
@RequestParam("name") String name,
@RequestParam("phone") String phone,
@RequestParam("address") String address) {
UserProfile userProfile = userMap.get(id);
userProfile.setName(name);
userProfile.setPhone(phone);
userProfile.setAddress(address);
}
//삭제
@DeleteMapping("user/{id}")
public void deleteUserProfile(@PathVariable("id") String id) {
userMap.remove(id);
}
}
728x90
'ETC.. > Spring' 카테고리의 다른 글
[Spring] Spring Boot로 ToDoApp만들기(투두 앱) (1) (0) | 2021.04.05 |
---|---|
[Spring] Spring Boot Junit 단위 테스트 설명 & 예제 (Kotlin) (0) | 2021.04.04 |
[Spring] Spring Boot 예외 처리 설명 & 예제 (Kotlin) (0) | 2021.03.17 |
[Spring] SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet (0) | 2021.03.16 |
[Spring] Spring Boot Validation 설명 & 예제 (Kotlin) (0) | 2021.03.12 |