5

Assuming the following class:

public class Foo {
  String a, b, c, d;

  // The rest of the class...
}

And a REST API controller using Springboot:

@GetMapping("/foo")
public Foo myFuntion() {
    return new Foo(...);
}

Requesting /foo return this JSON:

{
 "a": "...",
 "b": "...",
 "c": "...",
 "d": "..."
}

However, I would like to return only some attributes of the Foo class, for example, only attributes a and b.

How could I do that without creating a new class?

5
  • The idiomatic approach to solving this problem is to create DTO (Data transfer objects). Java doesn't much like "sometimes these values, sometimes not" since it makes for inconsistent APIs and breaks strong typing. Commented Jan 14, 2020 at 12:20
  • @Christopher Would that change if I always want to return only a and b? Commented Jan 14, 2020 at 12:22
  • In your existing DTO class, annotate that class with "@JsonInclude(JsonInclude.Include.NON_EMPTY)" and then set the attributes to null from foo before returning. Commented Jan 14, 2020 at 12:26
  • You could have a look at GraphQL where you'd be able to define what exactly should be returned. Commented Jan 14, 2020 at 12:36
  • If you never want to return a given property use @JsonIgnore on it. If you need to return different sets of properties in different places use DTOs. Commented Jan 14, 2020 at 12:38

3 Answers 3

6

You have two solutions

Use @JsonIgnore on the property you want to exclude

For example, You want to exclude a from serializing.(Just want to get b,c,d)

public class TestDto {

@JsonIgnore
String a;
String b;
String c;
String d;
//Getter and Setter
}

Use @JsonInclude and @JsonIgnoreProperties

Via this solution, if each of a,b,c,d is null, it will be excluded from the response.

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestDto {


String a;
String b;
String c;
String d;

//getters and setter

}

More information about Jackson annotations

Sign up to request clarification or add additional context in comments.

Comments

4

You have plenty of options

  1. Use dedicated DTO - separate class that contains only props you need
  2. Use @JsonIgnore
  3. Use @JsonView

... and there are many more. I personally am very comfortable with 3rd option https://www.baeldung.com/jackson-json-view-annotation - but most straight forward and implementation independent is option 1 - so you can go for it as well.

Comments

3

@JsonView will be the best option to handle all attributes in controlled way.

Define a view

public class Views {
    public static class Public {
    }

    public static class private {
    }
}

Map attributes

@JsonView(Views.Public.class)
public String a;

And mark the return view

@JsonView(Views.Public.class)
@RequestMapping("/items/{id}")
public Item getItemPublic(@PathVariable int id) {
    return ItemManager.getById(id);
}

Now all attributes marked with the view name will be returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.