본문 바로가기

개발 관련 기록과 정리

(23)
JSON 파일 데이터 가져오기 1 2 3 4 5 6 7 Resource resource = new ClassPathResource("MOCK_DATA.json"); try { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(resource.getInputStream(), Object.class); } catch (IOException e) { e.printStackTrace(); } Colored by Color Scripter cs
Spring Boot Validation Message 바꾸기 유효성 검사에서 메세지를 강제적으로 바꿀수 있다. https://stackoverflow.com/questions/6048556/can-you-change-an-annotation-message-at-run-time Can you change an annotation message at run time? I'm trying to include a dynamic message in my annotation that changes the main body of the text based on the values that are found in the other variables that are passed to it. I set a default mes... stackoverflow.com
Redirect와 Forward의 사용법(Spring Boot 2.X) Redirect를 사용 할때 아무 생각없이 사용 할때가 있다. 그러다가 의문이 든것이 redirect:/url 와 response.response.sendRedirect("/url")의 차이점이다. 결론은 전처리 후처리를 해주는 인터셉터에서 재처리를 하느냐 안하는가의 차이가 있었다. redirect:/url 의 경우, 인터셉터를 다시 요청해서 전후처리를 할 수 있다. 하지만 response.sendRedirect("/url") 의 경우 인터셉터를 다시 요청하지 않는다는 것이다. 만약에 인터셉터에서 권한 인증이 필요하면 절대로 response.response.sendRedirect("/url")를 사용하면 안될 것이다. Forward도 마찬가지이다. request.getRequestDispatcher("/..
Maven multi module에서 yml(yaml) 오버라이딩 설정 (spring boot 2.x) @PropertySources({ @PropertySource("application-share.yml"), @PropertySource("application.yml") }) @PropertySource(value = { "classpath:/application.yml", "classpath:/application-share.yml" }) 위 방식은 확장자가 properties의 경우, 사용 가능하다. yml(yaml)은 사용 불가능하다. 방법1. 메인이 되는 yml과 서브 yml를 가질 경우의 설정(서브 yml이 2개 이상이면 문제 발생하므로 비추천) 질문 : I have a Spring boot application that is divided in several modules. The main ..
스프링 부트(Spring boot) 유효성 검사(validation) 사용자(custom) 어노테이션(annotation) 생성 어노테이션으로 사용 할 클래스를 생성 한다. NumberSymbol.java (interface) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.example.demo.common.validator.defaults; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.validation...
스프링 부트(Spring Boot) 유효성 검사(Validation) 어노테이션(Annotation) 종류 아래의 검사와 일치 할 경우 에러를 발생 @NotEmpty – null 또는 공백 일 경우(String, Collection, Map, Array 사용가능) @NotBlank – null 또는 공백 일 경우(String만 사용 가능) @NotNull – null 일 경우 @AssertTrue / @AssertFalse – true 또는 false 일 경우 @Size – min(최소) max(최대) 크기를 벗어 날 경우 (String, Collection, Map, Array 사용 가능) @Min – 최소 보다 작을 경우 @Max – 최대 보다 클 경우 @Email – 이메일 형식이 아닐 경우 @Positive / @PositiveOrZero – 양수 또는 0 부터 양수가 아닐 경우 @Negative / ..
Spring에서 @Valid와 @Validated의 차이점 Spring에서 @Valid와 @Validated의 차이점 Spring은 두 가지 유효성 검사 메소드 인 Spring 유효성 검사와 JSR-303 bean 유효성 검사를 지원합니다. 둘 모두는 bean validator를 포함한 다른 위임자에게 위임하는 Spring 유효성 검사기를 정의함으로써 사용될 수 있습니다. 여태까지는 그런대로 잘됐다. 그러나 실제로 유효성 검사를 요청하는 메소드에 주석을 달 때, 그것은 또 다른 이야기입니다. 나는 이렇게 주석을 달 수있다. @RequestMapping(value = "/object", method = RequestMethod.POST) public @ResponseBody TestObject create(@Valid @RequestBody TestObject o..