Jackson 라이브러리를 쓰면,
Response를 클래스 단위로 지정시 json형태로 값을 리턴할 수 있다.
@Controller
public class JsonExController {
@RequestMapping(value = "/user.json", method = RequestMethod.GET)
@ResponseBody
public Response<User> getUserInfo(Locale locale, Model model) {
Response<User> result = new ResponseUnwrapper<User>();
User user = new User("1", "minho", 30);
user.addList("TEST1");
user.addList("TEST2");
result.setData(user);
return result;
}
// 결과 : {"code":200,"desc":"OK","name":"minho","age":30,"list":["TEST1","TEST2"]}
@RequestMapping(value = "/dept.json", method = RequestMethod.GET)
@ResponseBody
public Response<Dept> getDeptInfo(Locale locale, Model model) {
Response<Dept> result = new Response<Dept>();
Dept dept = new Dept("1", "cloud");
result.setData(dept);
return result;
}
// 결과 : {"code":200,"desc":"OK","data":{"deptName":"cloud"}}
@RequestMapping(value = "/deptList.json", method = RequestMethod.GET)
@ResponseBody
public Response<List<Dept>> getDeptInfoList(Locale locale, Model model) {
Response<List<Dept>> result = new Response<List<Dept>>();
List<Dept> deptList = new ArrayList<Dept>();
Dept dept1 = new Dept("1", "cloud");
Dept dept2 = new Dept("2", "mail");
Dept dept3 = new Dept("3", "cafe");
deptList.add(dept1);
deptList.add(dept2);
deptList.add(dept3);
result.setData(deptList);
return result;
}
// 결과 : {"code":200,"desc":"OK","data":[{"deptName":"cloud"},{"deptName":"mail"},{"deptName":"cafe"}]}
@RequestMapping(value = "/mapdata.json", method = RequestMethod.GET)
@ResponseBody
public Response<MapData<String, Object>> getMapData(Locale locale, Model model) throws JsonGenerationException, JsonMappingException, IOException {
Response<MapData<String, Object> > result = new Response<MapData<String, Object> >();
MapData<String, Object> data = new MapData<String, Object>();
data.add("name", "minho");
data.add("age", "30");
result.setData(data);
return result;
}
// 결과 : {"code":200,"desc":"OK","age":"30","name":"minho"}
@JsonPropertyOrder({ "code", "desc" }) // json 출력 순서지정
public class Response<T> {
@JsonProperty("code")
private int code = 200;
@JsonProperty("desc") // json key명 지정
private String desc = "OK";
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public class ResponseUnwrapper<T> extends Response<T> {
public ResponseUnwrapper() {
}
@Override
@JsonUnwrapped // 해당 객체를 한단계 위로 올린다 (결과 참조), jackson 2.0이후부터는 prefix도 지정가능
public T getData() {
return super.getData();
}
}
public class User {
private final String id;
private final String name;
private final int age;
private final List<String> list;
public User(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
this.list = new ArrayList<String>();
}
@JsonIgnore // json 출력에서 제외함
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void addList(String val) {
list.add(val);
}
public List<String> getList() {
return list;
}
}
public class Dept {
private final String id;
private final String deptName;
public Dept(String id, String deptName) {
this.id = id;
this.deptName = deptName;
}
@JsonIgnore
public String getId() {
return id;
}
public String getDeptName() {
return deptName;
}
}
// @JsonUnwrapped 어노테이션이 map에는 적용되지 않아 wrapper 클래스를 사용한다.
public class MapData<V, K> {
private final Map<V, K> map;
public MapData() {
map = new HashMap<V,K>();
}
@JsonAnySetter
public void add(V key, K value) {
map.put(key, value);
}
@JsonAnyGetter
public Map<V, K> getMap() {
return map;
}
}
'백엔드기술 > 스프링프레임워크' 카테고리의 다른 글
Java10으로 Spring Boot 실행시 (0) | 2018.09.02 |
---|---|
메이븐 사용자 라이브러리 추가하기 (0) | 2013.02.18 |
Spring Framework MVC 로그인 (0) | 2012.12.04 |