막상 JSON으로 만들 객체를 만들려니 헷갈려서 찾아보게 되었고 참고자료가 매우x1000000 도움이 되었다.
내가 하려고 한 작업은 아래와 같다.
각각 firstInput, secondInput, thirdInput으로 ID를 설정한 input 값을 가져 온 후
JSON 형태로 객체를 만들고 allInput이라는 hidden 설정 되어있는 input값에 넣은 후 Controller에 넘긴다.
JSON.stringify(); (JSON -> String 변환)
function getInputVal(){
var data = new Object();
data.first_input_value = $('#firstInput').val();
data.second_input_value = $('#secondInput').val();
data.third_input_value = $('#thirdInput').val();
var jsonData = JSON.stringify(data);
$('#allInput').val(jsonData);
}
Controller에서 로그를 찍어보면 아래처럼 String 타입으로 넘어가는 것을 확인할 수 있다.
public Page<StudentVo> seachList(List<Search> search, Pageable pageable) {
String startDate = null;
String endDate = null;
String[] orderBy = String.valueOf(pageable.getSort()).replaceAll(" ","").split(":");
String aa = orderBy[0] + " " + orderBy[1];
for(ColumnSearch searchVo : search) {
if(searchVo.getTargetColumn().equalsIgnoreCase("start")) {
startDate = searchVo.getSearchValue();
} else if(searchVo.getTargetColumn().equalsIgnoreCase("end")) {
endDate = searchVo.getSearchValue();
} else {
log.info("check start date or end date");
}
}
List<StudentVo> result = studentRepository.getStudentTotalScore(startDate, endDate);
Collections.sort(result);
List<StudentVo> sortResult = new ArrayList<>();
if(orderBy[1].equals("ASC")) {
switch(orderBy[0]) {
case "sudentId":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::sudentId)).collect(Collectors.toList());
break;
case "studentName":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::studentName)).collect(Collectors.toList());
break;
case "totalMathScore":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::totalMathScore)).collect(Collectors.toList());
break;
case "totalEngScore":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::totalEngScore)).collect(Collectors.toList());
break;
default:
log.info("ORDER BY ASC 컬럼명 오류");
break;
}
} else if(orderBy[1].equals("DESC")) {
switch (orderBy[0]) {
case "sudentId":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::sudentId).reversed()).collect(Collectors.toList());
break;
case "studentName":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::studentName).reversed()).collect(Collectors.toList());
break;
case "totalMathScore":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::totalMathScore).reversed()).collect(Collectors.toList());
break;
case "totalEngScore":
sortResult = result.stream().sorted(Comparator.comparing(StudentVo::totalEngScore).reversed()).collect(Collectors.toList());
break;
default:
log.info("ORDER BY DESC 컬럼명 오류");
break;
}
}
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize());
int start = (int) pageRequest.getOffset();
int end = Math.min((start + pageRequest.getPageSize()), sortResult.size());
Page<StudentVo> page = new PageImpl<>(sortResult.subList(start, end), pageRequest, sortResult.size());
log.info("Page : {} ", page.getContent());
}
return page;
}
열심히 삽질하다가 찾은 것이 Projection을 이용하는 방법이다.
먼저 필요한 기존VO에서 데이터들(DTO로 변환할 데이터)만 모은 인터페이스 VO를 생성한다.
Interface VO
package com.beige0905.test.repository.vo;
public interface IStudentVo {
int getStudentId();
String getStudentName();
int getTotalMathScore();
int getTotalEngScore();
}
기존 VO에 .map 함수를 이용하여 IStudentVo -> StudentVo로 Convert 해주는 method를 추가한다.