연습
보고 따라했습니다
get: 조회
post: 생성
put/petch: 업데이트
delete: 삭제
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.UserProfile;
//@RestController
//스프링 프레임웍이 알아서 이 컨트롤러를 컨트롤러로 인식하고 인스턴스 생성
//json 형태로 객체 데이터를 반환
@RestController
public class UserProfileController {
private Map<String, UserProfile> userMap; //C#의 Dictionary와 비슷하다
//객체 초기화
@PostConstruct
public void init() {
userMap = new HashMap<String, UserProfile>();
userMap.put("1", new UserProfile("1","홍길동","1111-1111","서울시 강남구 대치1동"));
userMap.put("2", new UserProfile("2","홍길자","1111-1112","서울시 강남구 대치2동"));
userMap.put("3", new UserProfile("3","홍길홍","1111-1113","서울시 강남구 대치3동"));
}
@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());
}
@PostMapping("/user/{id}")
public void postUserProfile(@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);
}
@PutMapping("/user/{id}")
public void putUserProfile(@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 deleteProfile(@PathVariable("id") String id) {
userMap.remove(id);
}
}
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) {
super();
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;
}
}
GET
/user/{id} ->/user/1
/user/all
POST
http://localhost:8080/user/4?name=홍홍홍&phone=123-4567&address=대한민
국 짤림..
PUT
http://localhost:8080/user/1?name=임꺽정&phone=987-6543&address=미국
Delete
http://localhost:8080/user/1
예전에 노드로 시험봤던 것도 도전
프로토콜 문서에서 요부분
package com.study.study1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PurchaseController {
@GetMapping("/purchase")
public String getPurchase() {
String result ="구매 내역 조회";
System.out.print(result);
return result;
}
@PostMapping("/purchase")
public String postPurchase() {
String result ="구매 내역";
System.out.print(result);
return result;
}
}
Annotation?
주석이라는 사전적 의미
@사인으로 시작
컴파일 또는 런타임에 해석
메타데이터(Data를 위한 데이터) 라고도 불리고 JDK5부터 등장
특정 클래스, 메소드, 변수 맨 위에 붙여 쓰며, 해당 구역의 기능을 확장하는 역할을 함
Map?
C#의 Dictionary 비슷
model ?
사용자 데이터를 처리하는 클래스 모음
필드에 멤버변수들 private로 선언해놓고
public 생성자, get, set 메서드 정의
Controller ?
사용자 api 요청을 처리해줌 (node에서 라우터)
@RestController
스프링 프레임웍이 알아서 이 컨트롤러를 컨트롤러로 인식하고 인스턴스 생성
json 형태로 객체 데이터를 반환
@PostConstruct
객체 생성된 뒤 초기화 작업 수행
'Web > Spring' 카테고리의 다른 글
Spring boot REST API, H2 DB(sql), JPA (1) + Entity, Lombok 생성자 에러 (0) | 2020.09.21 |
---|---|
Spring boot REST API + MySQL(오라클, mybatis xml 작성) (0) | 2020.09.20 |
Spring boot REST API + MySQL(오라클, mybatis) (0) | 2020.09.18 |
Spring boot - RESTful API(2) (0) | 2020.09.17 |
spring framework 설치 및 환경설정 (spring legacy project/tomcat) (0) | 2020.09.16 |