Search code examples
javaspringspring-bootspring-data-rest

Spring Boot REST application; findBy method using foreign key


Using Spring Data REST, I have two models, NvdApps with a one-to-many relationship to NvdVulnerabilities

I'm trying to add the ability to search NvdVulnerabilities by an NvdApp, so my repository looks like this:

public interface NvdVulnerabilityRepository extends PagingAndSortingRepository<NvdVulnerability, Long> {
    List<NvdVulnerability> findByNvdApp(NvdApp nvdApp);
}

And this gives me the REST end point:

"findByNvdApp" : {
      "href" : "http://localhost:8080/api/nvdVulnerabilities/search/findByNvdApp{?nvdApp}",
      "templated" : true
    }

When I try to use this end point, it just returns the entire table, regardless of what I put in the query string. Example URL's I've tried:

http://localhost:8080/api/nvdVulnerabilities/search/findByNvdApp?nvd_app_id=25
http://localhost:8080/api/nvdVulnerabilities/search/findByNvdApp?25
http://localhost:8080/api/nvdVulnerabilities/search/findByNvdApp?NvdApp=25

Am I missing some configuration? I'm basically trying to replicate the query:

SELECT * FROM NVD_VULNERABILITY where nvd_app_id = 25

Which works as intended in the H2 database console. How exactly does the search end point work, and how do I get it to filter as intended?

I'd also like to have the paging work with the search endpoint; right now it returns the entire 7k+ rows whereas the end point http://localhost:8080/api/nvdVulnerabilities/ returns 20 items per page


Solution

  • You can try:

    List<NvdVulnerability> findByNvdApp_Id(Integer id);
    

    If Integer id variable exists in your NvdApp class.