I'm looking for a way to export some JPA entities to a REST API, but instead of sending the whole entity every time I want to share just some specific fields depending of the entry point. Here's a small example:
Say we have an Author class with few fields:
@Entity
public class Author implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = SEQUENCE)
    private Long id;
    @NotNull
    @Size(min = 1, message = "{required.field}")
    private String name;
    @NotNull
    @Size(min = 1, message = "{required.field}")
    private String country;
    private LocalDate birthDate;
    // getters and setters
}
And say we have this REST service (just two methods):
@Path("authors")
public class AuthorREST {
    @Inject
    private AuthorBC bc;
    @GET
    @Produces("application/json")
    public List<Author> find(@QueryParam("q") String query) throws Exception {
        List<Author> result;
        if (Strings.isEmpty(query)) {
            result = bc.findAll();
        } else {
            result =  bc.find(query);
        }
        return result;
    }
    @GET
    @Path("{id}")
    @Produces("application/json")
    public Author load(@PathParam("id") Long id) throws Exception {
        Author result = bc.load(id);
        if (result == null) {
            throw new NotFoundException();
        }
        return result;
    }
}
Now, this way, I'll always have the 4 fields when my API is called.
I understand that if I use Jackson I can set an @JsonIgnore to fields I want to ignore, and they will always be ignored.
But what if I want that, in some cases, my whole entity is returned by one service, and in other service (or other method in the same service), only 2 or 3 fields are returned?
Is there a way to do it?
 
     
     
     
     
    