gregwhitaker / catnap

Partial JSON response framework for RESTful web services
Apache License 2.0
55 stars 15 forks source link

Spring Data - PagingAndSortingRepository with @CatnapResponseBody #17

Open onek13 opened 6 years ago

onek13 commented 6 years ago

I'm working on a Spring REST project and wanted to utilize catnap features.

However it seems not fully working if i use PagingAndSortingRepository though i found a work around for it.

I think would be nice if we can directly return the org.springframework.data.domain.Page from our RestControllers and still be able to do partial response filtering.

Please see details below

Sample response / expected response.

{
    "content": [
        {
            "name": "Name 1"
        },
        {
            "name": "Name 2"            
        }
    ],
    "size": 5,
    "number": 5,
    "last": true,
    "sort": null,
    "first": false,
    "numberOfElements": 2,
    "totalPages": 6,
    "totalElements": 0
}

Only content from sample response above can do partial response filtering, was not able to get some fields outside the content (eg. totalPages, totalElements) using the code below

@CatnapResponseBody
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public Page<User> getList(Pageable pageable) {
        return userService.getUserList(pageable);
    }

Below is the workaround code. Need to somehow copy the properties from org.springframework.data.domain.Page into my custom JsonResponseList

@CatnapResponseBody
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResponseList<User> getList(Pageable pageable) {
        JsonResponseList<User> jsonResponseList = new JsonResponseList<User>();
        Page<User> pageUser = userService.getUserList(pageable);
        BeanUtils.copyProperties(pageUser, jsonResponseList, "");
        return jsonResponseList;
    }
manuelarte commented 5 years ago

any updates?

manuelarte commented 5 years ago

JsonResponseList

can you paste the code of JsonResponseList

onek13 commented 5 years ago

@manuelarte

Please see code below.

public class JsonResponseList<T> {

    private List<T> m_content;
    private boolean m_first;
    private boolean m_last;
    private int m_number;
    private int m_numberOfElements;
    private int m_size;
    private String m_sort;
    private long m_totalElements;
    private int m_totalPages;

    public List<T> getContent() {
        return m_content;
    }

    public int getNumber() {
        return (m_number + 1);
    }

    public int getNumberOfElements() {
        return m_numberOfElements;
    }

    public int getSize() {
        return m_size;
    }

    public String getSort() {
        return m_sort;
    }

    public long getTotalElements() {
        return m_totalElements;
    }

    public int getTotalPages() {
        return m_totalPages;
    }

    public boolean isFirst() {
        return m_first;
    }

    public boolean isLast() {
        return m_last;
    }

    public void setContent(List<T> content) {
        m_content = content;
    }

    public void setFirst(boolean first) {
        m_first = first;
    }

    public void setLast(boolean last) {
        m_last = last;
    }

    public void setNumber(int number) {
        m_number = number;
    }

    public void setNumberOfElements(int numberOfElements) {
        m_numberOfElements = numberOfElements;
    }

    public void setSize(int size) {
        m_size = size;
    }

    public void setSort(String sort) {
        m_sort = sort;
    }

    public void setTotalElements(long totalElements) {
        m_totalElements = totalElements;
    }

    public void setTotalPages(int totalPages) {
        m_totalPages = totalPages;
    }

}