0

I'm working in a REST API (Java, SpringBoot, QueryDsl...) and I would like to customize the fields that I have in the result. By default I'm obtaining all fields of the entity but the fields that I need depends on the request. This should be something dynamic.

As far as I know, using projections I can obtain something like this but I have to declare previously the projection and I would like to work with something dynamic not static. On the other hand I have seen than something like GraphQL allows this behaviour but I would have to refactor everything.

Has anyone had this problem before?

This is my code:

BaseRestCRUDController

public abstract class BaseRestCRUDController<T extends EntityBase, V extends BaseDTO> {

@GetMapping("/list")
  public ResponseEntity<List<V>> findAll(Predicate predicate, Pageable pageable) {
    log.info("FindAll");
    return new ResponseEntity(getCRUDService().findAll(predicate, pageable), HttpStatus.OK);
  }

}

ExampleController

@RestController
@RequestMapping("/api/example")
public class ExampleController
    extends BaseRestCRUDController<Example, ExampleDTO> {

  @Autowired 
  private ExampleService ExampleService;

  @Override
  public ResponseEntity<List<ExampleDTO>> findAll(
      @QuerydslPredicate(root = Example.class) Predicate predicate, Pageable pageable) {
     return super.findAll(predicate, pageable);
  }

  @Override
  protected CRUDService<Example, ExampleDTO> getCRUDService() {
    return ExampleService;
  }
  
}

Example (Entity)

public class Example {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "name", nullable = false)
  private String name;

  @Column(name = "creation_date")
  private Instant creationDate;

  @Column(name = "last_update")
  private Instant lastUpdate;

  @Column(name = "erasure_date")
  private Instant erasureDate;
}
  • http://localhost:8080/api/example/list?name=test&page=0&size=5&sort=id,desc
  • http://localhost:8080/api/example/list?name=foo&page=1&size=10&sort=id,desc
  • http://localhost:8080/api/example/list?page=0&size=2&sort=id,asc
[
    {
        "id": 1,
        "name": "e1",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    },
    {
        "id": 2,
        "name": "e2",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    },
    {
        "id": 3,
        "name": "e3",
        "creationDate": "2021-11-15T23:00:00Z",
        "lastUpdate": null,
        "erasureDate": null
    }
]

How can I obtain something like this without use projections?

http://localhost:8080/api/example/list?fields=id,name&page=1&size=10&sort=id,desc

[
    {
        "id": 1,
        "name": "e1"
    },
    {
        "id": 2,
        "name": "e2"
    },
    {
        "id": 3,
        "name": "e3"
    }
]

http://localhost:8080/api/example/list?fields=name&page=1&size=10&sort=id,desc

[
    {
        "name": "e1",
    },
    {
        "name": "e2",
    },
    {
        "name": "e3",
    }
]
2
  • What database are you using? Commented Nov 16, 2021 at 13:26
  • Hi! @SimonMartinelli, MySql Commented Nov 16, 2021 at 14:34

1 Answer 1

1
 @Ignore
 private Instant creationDate;

Try this. You can use @Ignore on getter,setter or fields.

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

3 Comments

@Valbs This could be a workaround, but I need that the user selects the fields that they need in every moment.
For this you have to write your own classes which gets mapped to your result. As I can see from request you can filter out the logic easily
So this would be a case for graphql.org

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.