How to return a partial JSON response using Java?

后端 未结 3 707
孤街浪徒
孤街浪徒 2020-11-30 23:51

I\'m building a RESTful API and want to provide developers with the option to choose which fields to return in the JSON response. This blog post shows examples of how sever

3条回答
  •  清歌不尽
    2020-12-01 00:15

    The Library jersey-entity-filtering Can do that :

    https://github.com/jersey/jersey/tree/2.22.2/examples/entity-filtering-selectable

    https://jersey.java.net/documentation/latest/entity-filtering.html

    Exemple :

    My Object

    public class Address {
    
        private String streetAddress;
        private String region;
        private PhoneNumber phoneNumber;
    }
    

    URL

    people/1234?select=streetAddress,region

    RETURN

    {
       "streetAddress": "2 square Tyson",
       "region": "Texas"
    }
    

    Add to Maven

    
        org.glassfish.jersey.ext
        jersey-entity-filtering
        2.22.2
    
    

提交回复
热议问题