Spring/Spring 입문 (10) 썸네일형 리스트형 빈(Bean) 스코프 Spring에서 빈(Bean) 스코프는 스프링 컨테이너가 관리하는 빈의 생성 범위를 지정하는 개념입니다. 쉽게 말해, 특정 빈이 어떤 범위에서, 얼마나 오래 유지되는지를 결정하는 설정입니다. 스프링 빈 스코프 종류Spring은 다음과 같은 여러 가지 빈 스코프를 제공합니다. 스코프 종류설명사용 위치singleton(기본값) 하나의 인스턴스를 애플리케이션 내에서 공유스프링 컨테이너 전체prototype요청할 때마다 새로운 인스턴스를 생성스프링 컨테이너 전체requestHTTP 요청마다 새로운 인스턴스를 생성웹 애플리케이션 (Spring MVC)sessionHTTP 세션마다 새로운 인스턴스를 생성웹 애플리케이션 (Spring MVC)application애플리케이션(ServletContext) 당 하나의 인.. 7. Validation Validation, 검증이란 프로그래밍에 있어서 가장 필요한 부분이다. 특히 Java에서는 null 값에 대해서 접근 하려고 할 때 null pointer exception이 발생 함으로, 이러한 부분을 방지 하기 위해서 미리 검증을 하는 과정을 Validation 이라고 한다. 단순하게는 아래와 같은 코드들이 있다. 특정한 클래스 객체가 들어온다고 하던지 반복된다면 여러가지 문제점이 야기된다. 1. 검증해야 할 값이 많은 경우 코드의 길이가 길어진다. 2. 구현에 따라서 달라 질 수 있지만 Service Logic과의 분리가 필요 하다. 3. 흩어져 있는 경우 어디에서 검증을 하는지 알기 어려우며, 재사용의 한계가 있다. 4. 구현에 따라 달라 질 수 있찌만, 검증 Logic이 변경 되는 경우 테스트.. 8. Object Mapper Object mapper는 Text Json을 Object로 바꿔주고 Object를 Json으로 바꿔준다. 컨트롤러에서 Json 요청이 들어오면 object로 바꿔주고 response object를 Json(text)로 변환해왔다. 이 것을 직접 객체로 생성해서 활용할 수 있는 방법을 알아보았다. package com.example.objectmapper; public class User { private String name; private int age; public User(String name, int age){ this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + nam.. 7. Response Response를 내리는 다양한 방법. import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class ApiController { @GetMapping("/text") public String text(@RequestParam String .. 6. 리소스 삭제 DELETE DELETE package com.example.delete.controller; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api") public class DeleteApiController { @DeleteMapping("/delete/{userid}") public void delete(@PathVariable String userid, @RequestParam String account){ System.out.println(userid); System.out.println(account); } } Delete는 get과 동작이 비슷하다. 데이터가 있던 없든 요청이 정확하다면 200ok를 돌.. 5. PUT API PUT 은 첫 한번 데이터가 생성되고 그 다음에는 계속 데이터가 업데이트 되기 때문에 데이터는 항상 하나이므로 멱등하다고 할 수 있다. 잘못된 데이터도 전송하기 때문에 안정성은 낮고 Query Parameter는 따로 설계하지 않는 것이 권장된다. import com.example.put.dto.PostRequestDto; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bin.. 4. POST API WEB에서는 json이나 xml 형태로 주고 받는다. 최근에는 json을 많이 쓴다. JSon 데이터 종류 String : value number : value boolean : value { } object : value array : value [ ] phone_number => 이런식으로 단어를 연결 할 때 _ 붙이는 것을 스네이크 케이스 phoneNumber => 이런식으로 단어를 하는 것은 카멜 케이스(낙타봉 형태) Post일때 @RequestBody를 꼭 붙여주자 @RestController @RequestMapping("/api") public class PostApiController { @PostMapping("/post") public void post(@RequestBody Map r.. 3. GET API GET은 리소스를 취득하는 API이며 쿼리 파라미터를 가질 수 있다. @GetMapping에 default로 값을 입력하면 path로 등장한다. 명시적으로 등록하려면 (path = " 값 " 을 넣으면 된다. @RequestMapping("/hi") // get / post / put / delete public String hi(){ 이런식으로 지정하면 모든 메서드에 동작이 되므로 명시적으로 지정해줘야 한다. @RequestMapping(path = "/hi", method = RequestMethod.GET) // get http://localhost:9090/api/get/hi @RequestMapping을 이와같이 사용하는 것은 예전에 활용하던 주소 매핑 방식. 이 방식을 합친 것이 @GetMap.. 이전 1 2 다음