helloworld
버전관리 본문
이번엔 내 코드에 대해 버전 관리를 해보자 .. 생각보다 어렵지는 않더군 ..
우선 기존 도메인클래스인 User클래스를 복사해서 버전2라는 의미의 UserV2라는 도메인을 생성했다.
그 후 기존 도메인을 상속 받은 뒤 String grade를 추가해주었다.
*디폴트생성자가 없으면 에러가 날지도...
컨트롤러에서
@GetMapping("/users/{id}")
public MappingJacksonValue retrieveUserV1(@PathVariable int id) {
User user = service.findOne(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
.filterOutAllExcept("Json필터에서 사용하는 변수들");
FilterProvider filters = new SimpleFilterProvider().addFilter("Json필터의 엔드포인트",filter);
MappingJacksonValue mapping = new MappingJacksonValue(user);
mapping.setFilters(filters);
return mapping;
}
public MappingJacksonValue retrieveUserV1(@PathVariable int id) {
User user = service.findOne(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
.filterOutAllExcept("Json필터에서 사용하는 변수들");
FilterProvider filters = new SimpleFilterProvider().addFilter("Json필터의 엔드포인트",filter);
MappingJacksonValue mapping = new MappingJacksonValue(user);
mapping.setFilters(filters);
return mapping;
}
요 코드에서 user앞에 v1이라는 버전을 달아주고 복사하여 v2라는 버전도 만들었다.
@GetMapping("/v2/users/{id}")
public MappingJacksonValue retrieveUserV2(@PathVariable int id) {
User user = service.findOne(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
//User -> User2
UserV2 userV2 = new UserV2();
BeanUtils.copyProperties(user, userV2);//id, name, joinDate,password,ssn
userV2.setGrade("grade를 추가");
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
.filterOutAllExcept("Json필터에서 사용하는 변수들");
FilterProvider filters = new SimpleFilterProvider().addFilter("Json필터의 엔드포인트",filter);
MappingJacksonValue mapping = new MappingJacksonValue(userV2);
mapping.setFilters(filters);
return mapping;
}
public MappingJacksonValue retrieveUserV2(@PathVariable int id) {
User user = service.findOne(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
//User -> User2
UserV2 userV2 = new UserV2();
BeanUtils.copyProperties(user, userV2);//id, name, joinDate,password,ssn
userV2.setGrade("grade를 추가");
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
.filterOutAllExcept("Json필터에서 사용하는 변수들");
FilterProvider filters = new SimpleFilterProvider().addFilter("Json필터의 엔드포인트",filter);
MappingJacksonValue mapping = new MappingJacksonValue(userV2);
mapping.setFilters(filters);
return mapping;
}
BeanUtils라는 클래스에서 copyProperties를 이용해 user를 userV2로 복붙해주었고
userV2에 grade를 set 하여 값을 넣어주었다.
그 후 실행 시키면 버전1과는 다른 내용이 출력된다.
*나는 MappingJacksonValue에 user를 수정하지 않아서 에러를 꽤 오래 찾았다. ㅋㅋ 복붙할때는 항상 유의하자..!
'restful' 카테고리의 다른 글
| jpa/ h2 (0) | 2023.08.24 |
|---|---|
| HATEOAS (0) | 2023.08.17 |
| 데이터 필터링 심화과정 (0) | 2023.08.13 |
| 데이터 필터링 (0) | 2023.08.13 |
| JSON 형태의 데이터를 xml 데이터로 변환하기 (0) | 2023.08.13 |