uri를 이용한 rest api 버전관리
This commit is contained in:
@@ -3,16 +3,10 @@ package com.example.restfulwebservice.user;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
@@ -37,8 +31,9 @@ public class AdminController {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public MappingJacksonValue retrieveUser(@PathVariable int id) {
|
||||
// GET /admin/user/1 -> version 추가
|
||||
@GetMapping("/v1/users/{id}")
|
||||
public MappingJacksonValue retrieveUserV1(@PathVariable int id) {
|
||||
User user = userDaoService.findOne(id);
|
||||
|
||||
if (user == null) {
|
||||
@@ -54,4 +49,26 @@ public class AdminController {
|
||||
mapping.setFilters(filters);
|
||||
return mapping;
|
||||
}
|
||||
@GetMapping("/v2/users/{id}")
|
||||
public MappingJacksonValue retrieveUserV2(@PathVariable int id) {
|
||||
User user = userDaoService.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);
|
||||
userV2.setGrade("VIP");
|
||||
|
||||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
|
||||
.filterOutAllExcept("id", "name", "joinDate", "grade");
|
||||
|
||||
FilterProvider filters = new SimpleFilterProvider().addFilter("UserInfoV2", filter);
|
||||
|
||||
MappingJacksonValue mapping = new MappingJacksonValue(userV2);
|
||||
mapping.setFilters(filters);
|
||||
return mapping;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.restfulwebservice.user;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.Past;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@JsonFilter("UserInfoV2")
|
||||
public class UserV2 extends User {
|
||||
private String grade;
|
||||
}
|
||||
Reference in New Issue
Block a user