ResponseEntity 에 Generic type 사용하기

By | 2020년 11월 15일
Table of Content

ResponseEntity 에 Generic type 사용하기

Generic type

@Getter
@NoArgsConstructor
public class MenusSaveRequestDto implements Serializable {

    private Integer pidx;
    private String menuName;
    private String useyn;

    @Builder
    public MenusSaveRequestDto(Integer pidx, String menuName, String useyn) {
        this.pidx = pidx;
        this.menuName = menuName;
        this.useyn = useyn;
    }

    @Builder
    public MenusSaveRequestDto(Integer pidx, String menuName) {
        this.pidx = pidx;
        this.menuName = menuName;
        this.useyn = "Y";
    }
}
@Getter
@Builder
public class MenusResponseDto implements Serializable {

    private final Integer cidx;
    private final Integer pidx;
    private final String menuName;
    private final String useyn;
    private final LocalDateTime regdate;
    private final LocalDateTime lastupdate;
}
@Getter
@AllArgsConstructor
public class ApiResponseMessage {
    private final String code;
    private final String message;
}
@Getter
@Setter
public class ApiResponseWithData<T> extends ApiResponseMessage {

    private T data;

    @Builder
    public ApiResponseWithData(String code, String message, T data) {
        super(code, message);
        this.data = data;
    }
}
@Getter
@Setter
public class ApiResponseWithPaging<T> extends ApiResponseWithData<List<T>> {

    private int pageNo;
    private int pageSize;

    private int resultCount;
    private int totalCount;
    private int totalPages;

    private boolean isFirst;
    private boolean isLast;

    @Builder(builderMethodName = "ApiResponseWithPagingBuilder")
    public ApiResponseWithPaging(String code, String message, List<T> data, int pageNo, int pageSize, int resultCount, int totalCount, int totalPages, boolean isFirst, boolean isLast) {
        super(code, message, data);

        System.out.println(3333);
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.resultCount = resultCount;
        this.totalCount = totalCount;
        this.totalPages = totalPages;
        this.isFirst = isFirst;
        this.isLast = isLast;
    }
}

ResponseEntity 와 Generic type

@Controller
public class MenuIndexController {

    final String TARGET_URI = "http://localhost:8081/v1/settings/menu";

    private final RestTemplate restTemplate;

    // ......

    @GetMapping("/settings/menu/")
    public String index() {

        String URL_SAVE = TARGET_URI;
        String URL_UPDATE = TARGET_URI + "/5";
        String URL_GET_ONE = TARGET_URI + "/5";
        String url = TARGET_URI + "/5";

        MenusSaveRequestDto menusSaveRequestDto = MenusSaveRequestDto.builder()
                .pidx(0)
                .menuName("메뉴 하나더 55555aaaa")
                .build();

        try {
            // 저장
            HttpEntity<?> httpEntity = new HttpEntity<Object>(menusSaveRequestDto);
            ResponseEntity<ApiResponseWithData<MenusResponseDto>> res_post
                    = restTemplate.exchange(
                    URL_SAVE,
                    HttpMethod.POST,
                    httpEntity,
                    new ParameterizedTypeReference<ApiResponseWithData<MenusResponseDto>>() {});
            System.out.println(res_post.getBody().getCode());
            System.out.println(res_post.getBody().getMessage());
            MenusResponseDto data_post = res_post.getBody().getData();
            System.out.println(data_post.getCidx());

            // 삭제
            String URL_DELETE = TARGET_URI + "/" + data_post.getCidx();
            restTemplate.delete(URL_DELETE);

            // 수정
            restTemplate.put(URL_UPDATE, menusSaveRequestDto);

            // 조회
//            res = restTemplate.getForEntity(URL_GET_ONE, ApiResponseWithData.class);
            ResponseEntity<ApiResponseWithData<MenusResponseDto>> res_get
                    = restTemplate.exchange(
                    URL_GET_ONE,
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<ApiResponseWithData<MenusResponseDto>>() {});
            System.out.println(res_get.getBody().getCode());
            System.out.println(res_get.getBody().getMessage());
            MenusResponseDto data_get = res_get.getBody().getData();
            System.out.println(data_get.getCidx());

            // 검색
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(URL_SEARCH)
                    .queryParam("pgno", "4");
            ResponseEntity<ApiResponseWithPaging<MenusResponseDto>> res_search
                    = restTemplate.exchange(
                    builder.toUriString(),
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<ApiResponseWithPaging<MenusResponseDto>>() {});
            System.out.println(res_search.getBody().getCode());
            System.out.println(res_search.getBody().getMessage());
            List<MenusResponseDto> data_search = res_search.getBody().getData();
            System.out.println(data_search.size());
            System.out.println(data_search.get(0).getCidx());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "settings/menu/index";
    }
}

댓글 남기기