본문 바로가기

개발

[웹 백엔드 시스템 구현 스터디] 1주차 기록

1. Controller vs RestController

1. @Controleller

 

1.1 SpringMVC 구조에서 VIEW를 반환하기 위해 사용

 

1.2 Data 반환 (Json 형태)

@ResponseBody 애노테이션 추가

 

2. @RestController

- SpringnMVC Controller에 ResponseBody 추가 된 형태 

- 용도: Json의 형태로 데이터 반환

- 반환시 주의사항: 데이터와 상태 코드를 함께 반환할것.

// 예.
return ResponseEntity.ok(user)

 

2. ResponseEntity

응답 값 + 응답 코드 반환 

 

3. Component 애노테이션

Bean으로 객체 등록

위와 같은 에러가 났다. 

 

이유

@Component 애노테이션 붙이자. 

 

4. serializer 에러 No serializer found for class com.github.prgrms.socialserver.model.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

원인: User 객체의 property는 모두 private이다. 

해결방법: JsonAutoDetect 애노테이션 추가 

 

5. MockMVC

서블릿 컨테이너의 구동 없이 테스트 환경에서 HTTP 서블릿 요청을 실행할 수 있도록 해주는 객체 

 

6. Configuration error: found multiple declarations of @BootstrapWith

원인: 테스트 클래스 상단에 @SpringBootTest와 @WebMvc 같이 쓸 경우 발생

해결: @SpringBootTest + @AutoConfigureMockMvc 조합으로 바꾼다.

 

7. MockMvc 한글 깨짐 현상

해결법: 아래 링크 

(BeforeEach에서 mockMvc에 대해 필터 추가)

https://milenote.tistory.com/58?category=963202

 

레퍼런스

1. Controller와 RestController차이

https://mangkyu.tistory.com/49

 

[Spring] @Controller와 @RestController 차이

Spring에서 컨트롤러를 지정해주기 위한 어노테이션은 @Controller와 @RestController가 있습니다. 전통적인 Spring MVC의 컨트롤러인 @Controller와 Restuful 웹서비스의 컨트롤러인 @RestController의 주요한 차..

mangkyu.tistory.com

 

2. ResponseEntity

https://tecoble.techcourse.co.kr/post/2021-05-10-response-entity/

 

ResponseEntity - Spring Boot에서 Response를 만들자

웹 서비스에서는 많은 정보를 송수신하게 됩니다. 각각의 다른 웹 서비스들이 대화하려면, 서로 정해진 약속에 맞게 데이터를 가공해서 보내야합니다. 보내는 요청 및 데이터의 형식을 우리는 H

tecoble.techcourse.co.kr

https://a1010100z.tistory.com/106

 

[Spring] ResponseEntity는 왜 쓰는 것이며 어떻게 쓰는걸까?

기존 내 개인 프로젝트 코드의 RestController 반환값은 모두 Object 타입이었다. 하지만, 일반적인 API는 반환하는 리소스에 Value만 있지 않다는 것을 모두 알고 있을 것이다. 당장 생각나는 것으로는

a1010100z.tistory.com

6. BootStrapWith 중복 선언 문제

https://stackoverflow.com/questions/41057602/spring-boot-1-4-testing-configuration-error-found-multiple-declarations-of-b

 

Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith

By following the official doc here: http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing i wanted to test one of my REST API method like this: @RunWith(SpringRunner.class) @

stackoverflow.com