Skip to content

Spring Page Jackson

정명주(myeongju.jung) edited this page Feb 2, 2018 · 2 revisions

Pages

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * spring data page 유틸리티
 *
 * @author myeongju.jung
 */
public final class Pages {
    private static final Page EMPTY = new PageImpl<>(Collections.emptyList());

    /**
     * 비어있는 페이지 반환
     *
     * @param pageable 페이징파라미터
     * @param <T>      페이지 항목 타입
     * @return 비어있는 페이지 객체
     */
    public static <T> Page<T> empty(Pageable pageable) {
        return new PageImpl<>(Collections.emptyList(), pageable, 0L);
    }

    @SuppressWarnings("unchecked")
    public static <T> Page<T> empty() {
        return EMPTY;
    }

    @SuppressWarnings("WeakerAccess")
    public static class Wrapper<T> extends PageImpl<T> {
        private static final long serialVersionUID = -8656698053747410891L;
        private static final Wrapper WRAPPER_EMPTY = new Wrapper<>(Collections.emptyList(), null, 0L);

        @JsonCreator
        public Wrapper(@JsonProperty("content") List<T> content,
                       @JsonProperty("number") int number,
                       @JsonProperty("size") int size,
                       @JsonProperty("totalElements") Long totalElements,
                       @JsonProperty("sort") List<OrderWrapper> orders
                      ) {
            super(content,
                  new PageRequest(number, toSize(size), toSort(orders)),
                  totalElements);
        }

        public Wrapper(List<T> content, Pageable pageable, long totalElements) {
            super(content, pageable, totalElements);
        }

        /**
         * 비어있는 객체 반환
         *
         * @param pageable 페이징 파라미터
         * @param <T>      페이지 항목 타입
         * @return 비어있는 페이지 객체
         */
        public static <T> Wrapper<T> empty(Pageable pageable) {
            return new Wrapper<>(Collections.emptyList(), pageable, 0L);
        }

        @SuppressWarnings("unchecked")
        public static <T> Wrapper<T> empty() {
            return WRAPPER_EMPTY;
        }

        private static int toSize(int size) {
            if (size == 0) {
                return 20;
            }
            return size;
        }

        private static Sort toSort(List<OrderWrapper> orders) {
            if (orders == null) {
                return null;
            }
            return new Sort(orders.stream().map(Sort.Order.class::cast).collect(toList()));
        }
    }

    public static class OrderWrapper extends Sort.Order {
        private static final long serialVersionUID = -4171265068578774315L;

        public OrderWrapper(@JsonProperty("direction") Sort.Direction direction,
                            @JsonProperty("property") String property,
                            @JsonProperty("nullHandling") Sort.NullHandling nullHandling) {
            super(direction, property, nullHandling);
        }
    }
}

Spring Data Page Deserialize

@Configuration("jacksonCustomConfiguration")
public class JacksonConfiguration {
    @Bean
    public Module pageMixInModule() {
        return new PageMixInModule();
    }

    @JsonDeserialize(as = Pages.Wrapper.class)
    private interface PageMixIn{ }

    private class PageMixInModule extends SimpleModule {
        private static final long serialVersionUID = -218026757469301909L;

        @Override
        public void setupModule(SetupContext context) {
            context.setMixInAnnotations(Page.class, PageMixIn.class);
        }
    }
}
Clone this wiki locally