json filter
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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.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")
|
||||
public class AdminController {
|
||||
private final UserDaoService userDaoService;
|
||||
|
||||
public AdminController(UserDaoService userDaoService) {
|
||||
this.userDaoService = userDaoService;
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public MappingJacksonValue retrieveAllUsers() {
|
||||
List<User> users = userDaoService.findAll();
|
||||
|
||||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
|
||||
.filterOutAllExcept("id", "name", "joinDate", "ssn");
|
||||
|
||||
FilterProvider filters = new SimpleFilterProvider().addFilter("UserInfo", filter);
|
||||
|
||||
MappingJacksonValue mapping = new MappingJacksonValue(users);
|
||||
mapping.setFilters(filters);
|
||||
return mapping;
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public MappingJacksonValue retrieveUser(@PathVariable int id) {
|
||||
User user = userDaoService.findOne(id);
|
||||
|
||||
if (user == null) {
|
||||
throw new UserNotFoundException(String.format("ID[%s] not found", id));
|
||||
}
|
||||
|
||||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
|
||||
.filterOutAllExcept("id", "name", "joinDate", "ssn");
|
||||
|
||||
FilterProvider filters = new SimpleFilterProvider().addFilter("UserInfo", filter);
|
||||
|
||||
MappingJacksonValue mapping = new MappingJacksonValue(user);
|
||||
mapping.setFilters(filters);
|
||||
return mapping;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.restfulwebservice.user;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -13,7 +14,7 @@ import java.util.Date;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(value = {"password", "ssn"})
|
||||
@JsonFilter("UserInfo")
|
||||
public class User {
|
||||
private Integer id;
|
||||
@Size(min = 2, message = "Name은 2글자 이상 입력해 주세요.")
|
||||
|
||||
Reference in New Issue
Block a user