{"id":303,"date":"2018-01-10T21:41:39","date_gmt":"2018-01-10T21:41:39","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=303"},"modified":"2018-05-28T04:48:15","modified_gmt":"2018-05-28T04:48:15","slug":"spring-boot-spring-data-rest-and-query-parameters","status":"publish","type":"post","link":"https:\/\/kevinmichaelcoy.com\/blog\/2018\/01\/10\/spring-boot-spring-data-rest-and-query-parameters\/","title":{"rendered":"Spring Boot, Spring Data REST and Query Parameters"},"content":{"rendered":"<p>I have Spring Data REST repository that takes query parameters. It uses a PagingAndSortingRepository and looks like this:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n@RepositoryRestResource<br \/>\n@CrossOrigin(origins = &quot;http:\/\/localhost:8000&quot;)<br \/>\npublic interface IRestaurantRepository extends PagingAndSortingRepository&lt;Restaurant, Long&gt; {<br \/>\n\tpublic Page&lt;Restaurant&gt; findByNameContaining(String name, Pageable pageable);<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>You would get an error if you were to execute the search command that is created for you with cURL.<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\n$ curl -X GET http:\/\/localhost:8181\/restaurants\/search\/findByNameContaining?name=Foo<br \/>\n{&quot;cause&quot;:null,&quot;message&quot;:&quot;Unable to detect parameter names for query method com.coysoft.wingspecial.repository.IRestaurantRepository.findByNameContaining! Use @Param or compile with -parameters on JDK 8.&quot;}<br \/>\n[\/code]<\/p>\n<p>The error is pretty useful. It gives us two options for being able to use query parameters. The first is straightforward. Just modify the code like so:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n@RepositoryRestResource<br \/>\n@CrossOrigin(origins = &quot;http:\/\/localhost:8000&quot;)<br \/>\npublic interface IRestaurantRepository extends PagingAndSortingRepository&lt;Restaurant, Long&gt; {<br \/>\n\tpublic Page&lt;Restaurant&gt; findByNameContaining(@Param(&quot;name&quot;) String name, Pageable pageable);<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>With this modification you&#8217;ll be able to get results. The second option involves modifying your build process. In my case, I am using Maven and I am in fact using Java 8. Therefore, I can modify the pom.xml to use this:<\/p>\n<p>[code language=&#8221;xml&#8221; highlight=&#8221;16&#8243;]<br \/>\n\t&lt;build&gt;<br \/>\n\t\t&lt;plugins&gt;<br \/>\n\t\t\t&lt;plugin&gt;<br \/>\n\t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n\t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;<br \/>\n\t\t\t&lt;\/plugin&gt;<br \/>\n\t\t\t&lt;plugin&gt;<br \/>\n\t\t\t\t&lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;<br \/>\n\t\t\t\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;<br \/>\n\t\t\t\t&lt;version&gt;3.3&lt;\/version&gt;<br \/>\n\t\t\t\t&lt;configuration&gt;<br \/>\n\t\t\t\t\t&lt;source&gt;1.8&lt;\/source&gt;<br \/>\n\t\t\t\t\t&lt;target&gt;1.8&lt;\/target&gt;<br \/>\n\t\t\t\t\t&lt;fork&gt;true&lt;\/fork&gt;<br \/>\n\t\t\t\t\t&lt;compilerArgs&gt;<br \/>\n\t\t\t\t\t\t&lt;arg&gt;-parameters&lt;\/arg&gt;<br \/>\n\t\t\t\t\t&lt;\/compilerArgs&gt;<br \/>\n\t\t\t\t&lt;\/configuration&gt;<br \/>\n\t\t\t&lt;\/plugin&gt;<br \/>\n\t\t&lt;\/plugins&gt;<br \/>\n\t\t&lt;finalName&gt;ROOT&lt;\/finalName&gt;<br \/>\n\t&lt;\/build&gt;<\/p>\n<p>[\/code]<\/p>\n<p>Both work the same for me. Using @Param is more backwards compatible and verbose, but using -parameters would work across the board.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have Spring Data REST repository that takes query parameters. It uses a PagingAndSortingRepository and looks like this: [code language=&#8221;java&#8221;] @RepositoryRestResource @CrossOrigin(origins = &quot;http:\/\/localhost:8000&quot;) public interface IRestaurantRepository extends PagingAndSortingRepository&lt;Restaurant, Long&gt; { public Page&lt;Restaurant&gt; findByNameContaining(String name, Pageable pageable); } [\/code] You would get an error if you were to execute the&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"https:\/\/kevinmichaelcoy.com\/blog\/2018\/01\/10\/spring-boot-spring-data-rest-and-query-parameters\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[279,234,280,281,235],"_links":{"self":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/303"}],"collection":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":7,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":310,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions\/310"}],"wp:attachment":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}