How to dynamically remove fields from a json response?

后端 未结 1 1649
广开言路
广开言路 2020-12-30 11:43

I\'m using spring to create a restful api, so far so good, my question is how to model the fields that go to the response dynamically?

Thats the controller that i\'m

相关标签:
1条回答
  • 2020-12-30 12:18

    I'm struggling with this requirement, too.

    The following code is a workaround using the JsonViews already mentioned by @beerbajay in the comment to your question:

    http://wiki.fasterxml.com/JacksonJsonViews

    I'm using Spring Boot 1.2.3.

    Views.java

    package demo;
    
    public class Views {
    
        static interface Public {}
    
        static interface Internal extends Public {}
    }
    

    Album.java

    package demo;
    
    import com.fasterxml.jackson.annotation.JsonView;
    
    public class Album {
    
        @JsonView(Views.Public.class)
        private String id;
    
        @JsonView(Views.Public.class)
        private String title;
    
        @JsonView(Views.Public.class)
        private String artist;
    
        @JsonView(Views.Internal.class)
        private String secret;
    
        public Album(String id, String title, String artist, String secret) {
            this.id = id;
            this.title = title;
            this.artist = artist;
            this.secret = secret;
        }
    
    }
    

    AlbumController.java

    package demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.ObjectWriter;
    
    @RestController
    public class AlbumController {
    
        @Autowired
        ObjectMapper mapper;
    
        @RequestMapping(value = "/album", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public String getAlbum() throws JsonProcessingException {
            Album foo = new Album("1", "foo", "John Doe", "secretProperty");
    
            // replace the following value with runtime logic of your choice,
            // e.g. role of a user
            boolean forInternal = false;
    
            ObjectWriter viewWriter;
            if (forInternal) {
                viewWriter = mapper.writerWithView(Views.Internal.class);
            } else {
                viewWriter = mapper.writerWithView(Views.Public.class);
            }
    
            return viewWriter.writeValueAsString(foo);
        }
    }
    

    So the key is to use jackson's ObjectMapper and ObjectWriter to generate a string of the json representation of your object.

    Feels kind of ugly to me, but works. Certainly the question remains how this scales when defining more than one RequestMapping.

    0 讨论(0)
提交回复
热议问题