반응형
Spring Rest API 사용시 결과 객체에서 선택적으로 몇 가지 변수만 응답해주는 경우가 자주 발생합니다.
이런 경우 몇 가지 방법이 있습니다.
1. 전용 응답객체를 별도로 만들어서 사용한다.
2. Json Filter를 이용해 원하는 값만 보이게 한다.
3. @JsonView 어노테이션을 이용해 필터링 한다.
그 중 3번에 대한 사용법이 사용예시 입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.fasterxml.jackson.annotation.JsonView; | |
import lombok.Data; | |
@RestController | |
public class JsonViewTestController { | |
@GetMapping("/api/user") | |
@JsonView(ApiJsonView.class) | |
public User getUser() { | |
User user = new User(); | |
user.setUserName("흑고니"); | |
user.setPassword("들켜선안ㄷ.."); | |
return user; | |
} | |
@Data | |
public static class User { | |
@JsonView(ApiJsonView.class) | |
private String userName; | |
private String password; | |
} | |
public static class ApiJsonView { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.util.Assert; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.MapperFeature; | |
import com.fasterxml.jackson.databind.json.JsonMapper; | |
import JsonViewTestController.User; | |
@SpringBootTest | |
public class JsonViewTestControllerTest { | |
@Autowired | |
private JsonViewTestController jsonViewTestController; | |
@Test | |
public void getUserTest() throws JsonProcessingException { | |
final User user = jsonViewTestController.getUser(); | |
final String strUser = JsonMapper.builder()// | |
.disable(MapperFeature.DEFAULT_VIEW_INCLUSION)// | |
.build()// | |
.writerWithView(JsonViewTestController.ApiJsonView.class)// | |
.writeValueAsString(user); | |
Assert.isTrue(strUser.equals("{\"userName\":\"흑고니\"}"), "패스워들 들켰.."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"userName":"흑고니"} |
반응형
'java' 카테고리의 다른 글
아마존 리눅스 open jdk 17 설치 (2023-10-19) (0) | 2023.10.19 |
---|---|
Cannot load from short array because "sun.awt.FontConfiguration.head" is null (0) | 2023.03.30 |
Aes256Util.java - AES/GCM/NoPadding (0) | 2023.03.28 |
spring custom ErrorController (0) | 2022.11.16 |
사용자 정의 JsonSerializer (0) | 2022.09.27 |
spring 최초 한번 실행. (0) | 2022.09.07 |
스프링부트 jackson response date 타입 변환 설정 (0) | 2022.09.02 |
CalUtil.java 계산을 도와주는 유틸. (0) | 2022.02.11 |