Home > Enterprise >  JsonIgnore on field in list of objects
JsonIgnore on field in list of objects

Time:09-09

I have applied JsonIgnore annotation on a field in class.

public class User {

private String name;

@JsonIgnore
private String id;

public String getName(){ return this.name;}
public void setName(String name){ this.name = name; }

public String getId() { return this.id; }
public void setId(String id) {this.id = id; }

}

When the class object is returned as single object, the field gets ignored.

public ResponseEntity<User> get() {.... some code to get user and return User object}

However, when the list of object is returned as response, the field is not getting ignored and still present in response.

ProjectController.java
@RequestMapping(value="/{projectId}/users", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<User>> list(
            @ApiParam(value = "projectId")
            @PathVariable UUID projectId) {
        return ok(projectsService.getUsers(projectId));
    }
ProjectService.java
public List<User> getUsers(UUID projectId) {

// Some code here to fetch the data from another container , so framing URI here and sending the req
            String response = HttpUtils.getRequest(uri);
            return new ObjectMapper().readValue(response, List.class);
    }

Could you please help on how to ignore the field completely from sending in responses.

CodePudding user response:

Add @JsonIgnore to the fields (getter in this case) instead of directly to the property

@JsonIgnore
public String getId() { return this.id; }

CodePudding user response:

I assume that your response variable in ProjectService class looks something like this:

 String response = "[\n"  
                "  {\n"  
                "    \"id\": \"1\",\n"  
                "    \"name\": \"Test\"\n"  
                "  }\n"  
                "]";

You are trying to convert your json to a list, not a list of users, so change this line of code:

return new ObjectMapper().readValue(response, List.class);

to this:

new ObjectMapper().readValue(response, new TypeReference<List<User>>(){});

CodePudding user response:

I tried to reproduce your problem and couldn't. It worked for me. So may be it is the version of your library. Here is the versions I used:

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.2</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.13.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
          <version>2.13.2</version>
        </dependency>

And here is the code I ran:

private static void jsonParserIgnoreTest() {
    try {
        JsonPerson person = new JsonPerson(1L, "Jack");
        System.out.println("Single person: "   JsonUtils.writeObjectToJsonString(person));
        List<JsonPerson> list = new ArrayList<>();
        list.add(person);
        JsonGroup group = new JsonGroup("Interest Group", list);


        System.out.println("Group: "   JsonUtils.writeObjectToJsonString(group));
        System.out.println("List: "   JsonUtils.writeObjectToJsonString(list));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

And here is the output I got:

Single person: {"name":"Jack"}
Group: {"groupName":"Interest Group","people":[{"name":"Jack"}]}
List: [{"name":"Jack"}]

Here are the classes JsonPerson and JsonGroup (omitting setters and getters)

public class JsonPerson {
    @JsonIgnore
    private Long id;
    private String name;

    public JsonPerson() {
    }
    public JsonPerson(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class JsonGroup {
    String groupName;
    List<JsonPerson> people;

    public JsonGroup() {
    }

    public JsonGroup(String name, List<JsonPerson> people) {
        groupName = name;
        this.people = people;
    }

}

The class JsonUtils - a thin wrapper over Jackson-Json Library. It is part of Open source MgntUtils library written and maintained by me. If you are interested you can use it too. Here is JsonUtils Javadoc and the library could be obtained as Maven artifact On Maven central and on Github (including sources and Javadoc)

  • Related