목록Spring/Spring Boot (34)
똑같은 삽질은 2번 하지 말자
@ControllerAdvice, @RestControllerAdvice 둘다 @Component이기 때문에 자동으로 빈으로 등록되며, 기본적인 차이는 @Controller @RestController 의 차이와 비슷하다고 생각한다. ArrayIndexOutOfBoundsException이 발생하면 내가 만든 커스텀 핸들러로 처리를 한다. AppError 객체는 에러내용을 담기위해 임시로 만들어준 클래스 @RestControllerAdvice public class GlobalExceptionController { @ExceptionHandler(ArrayIndexOutOfBoundsException.class) public ResponseEntity AIOOBHandler(ArrayIndexOutOfB..
https://docs.spring.io/spring-restdocs/docs/2.0.2.RELEASE/reference/html5 Spring REST Docs Document RESTful services by combining hand-written documentation with auto-generated snippets produced with Spring MVC Test. docs.spring.io REST Docs -> 테스트를 하면서 문서 조각(snippets)를 만들 수 있으며, 이것들을 모아 문서화를 할 수 있다. 의존성은 프로젝트를 생성할 때 이미 추가했기 때문에 추가할 필요는 없고, REST Docs를 적용하기 위해서는 @AutoConfigureRestDocs 어노테이션을 추가하면..
HATEOAS 란? 어떤 계좌를 조회하는 요청이 있다. GET /accounts/12345 HTTP/1.1 Host: bank.example.com Accept: application/vnd.acme.account+json 그에 대한 응답 HTTP/1.1 200 OK Content-Type: application/vnd.acme.account+json Content-Length: ... { "account": { "account_number": 12345, "balance": { "currency": "usd", "value": 100.00 }, "links": { "deposit": "/accounts/12345/deposit", "withdraw": "/accounts/12345/withdraw", ..

지정된 값 이외의 값이 들어왔을때, 지정된 값이 들어오지 않았을 때 @Test public void createEvent_is_BadRequest() throws Exception { Event event = Event.builder() .id(100) // 들어오면 안되는 값 .name("Spring") .description("REST API") .beginEnrollmentDateTime(LocalDateTime.of(2020,6,28,14,11)) .closeEnrollmentDateTime(LocalDateTime.of(2020,6,29,14,11)) .beginEventDateTime(LocalDateTime.of(2020,6,30,14,11)) .endEventDateTime(LocalDat..
도메인 구현 @Builder @AllArgsConstructor @NoArgsConstructor // public default 생성자 @Getter @Setter // @Data에는 EqualsAndHashCode가 기본적으로 정의되어있어서 겹침 @EqualsAndHashCode(of = "id") // 기본적으로 객체의 모든 필드로 Equals, HashCode를 비교하는데 그럼 // 나중에 연관관계에서 (상호참조하는) StackOverFlow가 발생할 수 있으므로 ID로만 비교한다. public class Event { private Integer id; private String name; private String description; private LocalDateTime beginEnro..

https://www.youtube.com/watch?v=RP_f5dMoHFc 영상의 내용을 간략히 하면 현재 REST API라고 주장하는 REST API들은 조건들을 만족하지 않는다. 이다. 그럼 어떤 조건들을 만족하지 않는가? 1. Self-Describtive Message 2. HATEOAS(Hypermedia as the engine of application state) 이 두가지를 모두 만족하는 REST API는 드물다고 하신다. Self-descriptive message 메시지 스스로 메시지에 대한 설명이 가능해야 한다. 서버가 변해서 메시지가 변해도 클라이언트는 그 메시지를 보고 해석이 가능하다. 확장확장 가능한 가능한 커뮤니케이션 커뮤니케이션 HATEOAS 하이퍼미디어(링크)를 통해 ..

Spring Boot는 애플리케이션 운영환경에서 유용한 기능을 제공한다. 제공하는 기능중 엔드포인트와 매트릭스 그리고 데이터를 활용하는 모니터링 기능에 대해 알아보자. Actuator https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints Spring Boot Reference Documentation docs.spring.io JConsole 사용하기 https://docs.oracle.com/javase/tutorial/jmx/mbeans/ https://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html V..
Spring RestClient RestTemplate Blocking I/O 기반의 Synchronous API RestTemplateAutoConfiguration 프로젝트에 spring-web 모듈이 있다면 RestTemplateBuilder를 빈으로 등록해 줍니다. https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-client-access Integration As a lightweight container, Spring is often considered an EJB replacement. We do believe that for many, if not most, applicatio..

페이지가 두개있다. hello.html , my.html my페이지는 로그인 한 사용자에게만 접근 가능하게 하고 싶다. Spring Security를 이용해서 해보자. org.springframework.boot spring-boot-starter-security 추가해서 테스트를 돌려보면 @RunWith(SpringRunner.class) @WebMvcTest(HomeController.class) public class HomeControllerTest { @Autowired MockMvc mockMvc; @Test public void hello() throws Exception{ mockMvc.perform(get("/hello")) .andDo(print()) .andExpect(status()..

데이터베이스 초기화 테스트를 할때 @SpringBootTest 를 이용하면 test용의 properties가 없으면 전 범위 스코프에서 다 따오기 때문에 더 느릴수 있다는거 인지하자. (DataJpaTest는 슬라이스 테스트로써 Embedded DB만 설정해주면 빠르게 동작) JPA를 사용한 데이터베이스 초기화 spring.jpa.hibernate.ddl-auto spring.jpa.generate-dll=true로 설정 해줘야 동작함. SQL 스크립트를 사용한 데이터베이스 초기화 schema.sql 또는 schema-${platform}.sql data.sql 또는 data-${platform}.sql ${platform} 값은 spring.datasource.platform 으로 설정 가능. reso..