SPRING/스프링 MVC
-
전역 컨트롤러: @(Rest)ControllerAdviceSPRING/스프링 MVC 2021. 7. 13. 02:01
예외 처리, 바인딩 설정, 모델 객체를 모든 컨트롤러 전반에 걸쳐 적용하고 싶은 경우에 사용한다. - @ExceptionHandler - @InitBinder - @ModelAttributes 적용할 범위를 지정할 수도 있다. - 특정 애노테이션을 가지고 있는 컨트롤러에만 적용하기 - 특정 패키지 이하의 컨트롤러에만 적용하기 - 특정 클래스 타입에만 적용하기 @ControllerAdvice(assignableTypes = {SampleController2.class, EventApi.class}) // 2. @ControllerAdvice(SampleController2.class) // 3. @ControllerAdvice("com.springhm.demo") public class BaseContro..
-
예외 처리 핸들러: @ExceptionHandlerSPRING/스프링 MVC 2021. 7. 13. 00:43
특정 예외가 발생한 요청을 처리하는 핸들러 정의 - 지원하는 메소드 아규먼트 (해당 예외 객체, 핸들러 객체, ...) - 지원하는 리턴 값 - REST API의 경우 응답 본문에 에러에 대한 정보를 담아주고, 상태 코드를 설정하려면 ResponseEntity를 주로 사용한다. public class EventException extends RuntimeException{ } @Controller @SessionAttributes("event") public class SampleController2 { @ExceptionHandler({EventException.class, RuntimeException.class}) public String eventErrorHandler(EventException ..
-
DataBinder: @InitBinderSPRING/스프링 MVC 2021. 7. 11. 16:33
특정 컨트롤러에서 바인딩 또는 검증 설정을 변경하고 싶을 때 사용 @InitBinder public void initEventBinder(WebDataBinder webDataBinder) { webDataBinder.setDisallowedFields("id"); } 바인딩 설정 - webDataBinder.setDisallowedFields(); 포매터 설정 - webDataBinder.addCustomFormatter(); Validator 설정 - webDataBinder.addValidators(); 특정 모델 객체에만 바인딩 또는 Validator 설정을 적용하고 싶은 경우 - @InitBinder(“event”) @Controller @SessionAttributes("event") publ..
-
모델: @ModelAttribute 또 다른 사용법SPRING/스프링 MVC 2021. 7. 11. 02:57
@ModelAttribute의 다른 용법 - @RequestMapping을 사용한 핸들러 메소드의 아규먼트에 사용하기 (Parameter에 사용하는 방식 : 이전에 살펴봄) - @Controller 또는 @ControllerAdvice (이 애노테이션은 뒤에서 다룹니다.)를 사용한 클래스에서 모델 정보를 초기화 할 때 사용한다. - @RequestMapping과 같이 사용하면 해당 메소드에서 리턴하는 객체를 모델에 넣어 준다. = RequestToViewNameTranslator @Controller @SessionAttributes("event") public class SampleController2 { // 이름 Form 화면 @GetMapping("/events/form/name") public ..
-
핸들러 메소드 16부 - @ResponseBody & ResponseEntitySPRING/스프링 MVC 2021. 7. 10. 23:43
@ResponseBody - 데이터를 HttpMessageConverter를 사용해 응답 본문 메시지로 보낼 때 사용한다. - @RestController 사용시 자동으로 모든 핸들러 메소드에 적용 된다. ResponseEntity - 응답 헤더 상태 코드 본문을 직접 다루고 싶은 경우에 사용한다. @RestController @RequestMapping("/api/events") public class EventApi { @PostMapping @ResponseBody public Event createEvent(@RequestBody @Valid Event event, BindingResult bindingResult) { // save event // Binding 에러 있을 시에 Sys.out 으..
-
핸들러 메소드 15부: @RequestBody & HttpEntitySPRING/스프링 MVC 2021. 7. 10. 16:00
@RequestBody - 요청 본문(body)에 들어있는 데이터를 HttpMessageConveter를 통해 변환한 객체로 받아올 수 있다. - @Valid 또는 @Validated를 사용해서 값을 검증 할 수 있다. - BindingResult 아규먼트를 사용해 코드로 바인딩 또는 검증 에러를 확인할 수 있다. HttpMessageConverter - 스프링 MVC 설정 (WebMvcConfigurer)에서 설정할 수 있다. - configureMessageConverters: 기본 메시지 컨버터 대체 - extendMessageConverters: 메시지 컨버터에 추가 - 기본 컨버터 - WebMvcConfigurationSupport.addDefaultHttpMessageConverters Htt..
-
핸들러 메소드 14부 ResponseEntity 예제:) 다운로드SPRING/스프링 MVC 2021. 7. 10. 01:58
파일 리소스를 읽어오는 방법 - 스프링 ResourceLoader 사용하기 파일 다운로드 응답 헤더에 설정할 내용 - Content-Disposition: 사용자가 해당 파일을 받을 때 사용할 파일 이름 - Content-Type: 어떤 파일인가 - Content-Length: 얼마나 큰 파일인가 파일의 종류(미디어 타입) 알아내는 방법 - http://tika.apache.org/ org.apache.tika tika-core 1.26 ResponseEntity - 응답 상태 코드 - 응답 헤더 - 응답 본문 파일 다운로드 로직 @Controller public class FileController { @Autowired private ResourceLoader resourceLoader; @GetMa..
-
핸들러 메소드 13부: MultipartFileSPRING/스프링 MVC 2021. 7. 9. 19:13
MultipartFile - 파일 업로드시 사용하는 메소드 아규먼트 - MultipartResolver 빈이 설정 되어 있어야 사용할 수 있다. - POST multipart/form-data 요청에 들어있는 파일을 참조할 수 있다. - List 아큐먼트로 여러 파일을 참조할 수도 있다. @Controller public class FileController { @GetMapping("/file") public String fileUploadForm() { return "files/index"; } @PostMapping("/file") public String fileUpload(@RequestParam MultipartFile file, RedirectAttributes attributes) { //..