Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot Dynamic Querying With Spring Data Rest #11810

Closed
bliu13 opened this issue Jan 27, 2018 · 2 comments
Closed

Spring Boot Dynamic Querying With Spring Data Rest #11810

bliu13 opened this issue Jan 27, 2018 · 2 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@bliu13
Copy link

bliu13 commented Jan 27, 2018

Sorry, I don't mean to post the same issue here as well as on Stackoverflow, but I did not know I could post issues under spring-projects/spring-boot.

I am trying to do something a little interesting where I want to be able to dynamically build sql query filters using Spring Boot 1.5.9 and Spring Data Rest without needing to write a controller. I feel like I might be on the right path, but I am kinda stuck.

The idea is to intercept an HTTP Request GET method by using HandlerInterceptorAdapter and store the Request query parameters into an object that can be used by the PagingAndSortingRepository. The plan is to override findAll(Pageable p) that Spring Data Rest uses by default, to call findAll(Specification s, Pageable p) to allow dynamic filtering.

I have used Request Scope provided by Spring to save the query parameters to allow me to use them throughout the life cycle of the Request.

The problem is that I cannot access the QueryParam object from the Repository interface file. I tried to use dependency injection into the interface file, by following this stackoverflow solution, but it does not work: https://stackoverflow.com/questions/13815139/spring-dependency-injection-for-interfaces

Spring Data Rest tries to create an endpoint by trying to build a SQL query based on the function name. Even when I tried to get around this issue by trying to trick Spring Data by creating a function that it can query with then overriding it with SampleRepositoryComponents that I implemented below, I cannot get the data stored in QueryParam.

Someone else also made a pull request to allow Spring Data Rest to ignore certain methods: spring-projects/spring-data-rest#286

QueryParam:

public class QueryParam {
  String query_string;
  Map<String, String[]> parameter_map;

  public String getQuery_string() {
    return query_string;
  }
  public void setQuery_string(String query_string) {
    this.query_string = query_string;
  }
  public Map<String, String[]> getParameter_map() {
    return parameter_map;
  }
  public void setParameter_map(Map<String, String[]> parameter_map) {
    this.parameter_map = parameter_map;
  }
}

SampleRepository:

@RepositoryRestResource(collectionResourceRel = "Sample", path = "Sample")
public interface SampleRepository extends PagingAndSortingRepository<Sample, UUID>,
    JpaSpecificationExecutor<Sample> {
  // Cannot just use any custom name
  @RestResource(exported = false)
  public QueryParam getQuery_param();

  default public Page<Sample> findAll(Pageable p) {
    QuerySpecification<Sample> qs = new QuerySpecification<Sample>(getQuery_param());

    return findAll(qs, p);
  }
}

QuerySpecification:

public class QuerySpecification<T> implements Specification<T> {
  private QueryParam query_param;

  public QuerySpecification(QueryParam query_param) {
    this.query_param = query_param;
  };

  @Override
  public Predicate toPredicate(Root<T> rt, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    // Build your custom predicate here
    return null;
  }
}

SampleRepositoryComponents:

@Component
public abstract class SampleRepositoryComponents implements SampleRepository {
  @Autowired
  private QueryParam query_param;

  @Override
  public QueryParam getQuery_param() {
    return this.query_param;
  }

  public void setQuery_param(QueryParam query_param) {
    this.query_param = query_param;
  }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 27, 2018
@wilkinsona
Copy link
Member

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow where your question is only 30 minutes old. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

@wilkinsona wilkinsona added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 27, 2018
@bliu13
Copy link
Author

bliu13 commented Jan 27, 2018

Sorry, I should have waited a little longer, but there are clarifications that I need because I am not really sure if they are bugs or not. There are three issues and I am unsure if I would be able to get an answer from Stack Overflow and I haven't seen this in the Spring Documentation. If it is feasible, I do want to see if they can become enhancements.

  • I am trying to figure out if Spring dependency injections in interface files are actually possible. If it is actually possible and it's an intended behavior, is this supposed to work in the Repository interface file based on the second link in the original post?
  • I am also trying to figure out if Spring Data Rest is currently implemented to not allow ignoring of methods in the Repository interface.
  • Does Spring Data Rest use interface implementations with overrides that you have annotated as a component? The reason why this one is also important is because the solution in the second link in the original post seems to also imply that it's supposed to work, but it doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

3 participants