2

I'm working on a rest api project with spring boot and hibernate, and I'm wondering on json serialization of RestController using Jackson.

Here is the problem: I use external hibernate entities class defined in a library I cannot edit. This classes are very complex and define lot of field I'm not interested in when I return the object with the rest api.

Actually, I've solved the problem wrapping the original class with a wrapper class that exposes only the values I want to return from the controller.

Eg:

original class

class AccountEntity {
///...

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

/// ... Lot of code here
}

Wrapper class:

class AccountWrapper {

AccountEntity original;

public AccountWrapper(AccountEntity original) {
   this.original = original;
}

public String getName() {
   return this.original.getName();
}
}

and the use the Wrapper as following

@RestController("/api/user")
public class UsersController {
    @GetMapping("/")
    public AccountWrapper getUser() {
        AccountEntity account = //get account in some way
        AccountWrapper accountWrapper = new AccountWrapper(account);
        return accountWrapper;
    }
}

The method works well, but it's not very clean and makes stuff more complex (e.g., when I have to return lists), because I always have to wrap the original class.

I didn't found a method to make me able to specify which fields I want to serialize without modify (and I cannot) the original class.

Any help?

  • Generally you can use `MixIn` feature and define annotation for external classes you can not change. Take a look at: [Jackson parse json with unwraping root, but without ability to set @JsonRootName](https://stackoverflow.com/questions/19568867/jackson-parse-json-with-unwraping-root-but-without-ability-to-set-jsonrootname), [Jackson conditional @JsonUnwrapped](https://stackoverflow.com/questions/25425419/jackson-conditional-jsonunwrapped/25435990#25435990) – Michał Ziober Sep 20 '19 at 11:15

2 Answers2

2

Instead of using a wrapper class, create a DTO object for the rest API that will be leaner than the DB entity and a trasformer to create DTO from entity (and vice a verce)

The difference from using a wrapper here is that the DB entity is not part of the DTO, and thus does not need to be serialized on the response.

The big advantage here is that you separate the DB layer from the API layer, which makes it more flexible and easy to manage.

you can read more about this pattern here

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
0

Apparently, you can use Jackson Mixins to annotate a class with Jackson annotations.

See this answer for example.

The idea is to create an class with the annotations you want and to use objectMapper.getSerializationConfig().addMixInAnnotations() to register the MixIn with your class.

For example:

//Class you don't controll
public class User {
  private String name;
  private String password; //attribute we want to omit

  //... getters and setters
}
public abstract class UserMixIn {
  @JsonIgnore String getPassword();
}
objectMapper.addMixInAnnotations(User.class, UserMixIn.class);

Hope it helps,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31