title: How to consume a spring data rest service with java?
body: <p>I have the following spring boot + data Rest repository:</p>
  
  <pre><code>    @RepositoryRestResource(collectionResourceRel = "dto", path = "produtos")
      public interface ProdutoRepository extends CrudRepository&lt;Produto, Integer&gt; {
          @Query("SELECT p FROM Produto p where descricao LIKE CONCAT(UPPER(:like),'%')")
          List&lt;Produto&gt; findByLike(@Param("like") String like);
      }
  </code></pre>
  
  <p>I also have a java client that access this method (this is my example of doing it):</p>
  
  <pre><code>    String url = "http://localhost:8080/produtos/search/findByLike?like={like}";
      RestTemplate t = new RestTemplate();
      ProdutoDto resp = t.getForObject(url, ProdutoDto.class, txtLoc.getText());
  </code></pre>
  
  <p>ProdutoDto (this one is not totally necessary):</p>
  
  <pre><code>public class ProdutoDto extends HalDto&lt;Produto&gt; {}
  </code></pre>
  
  <p>HalDto:</p>
  
  <pre><code>public class HalDto&lt;T extends ResourceSupport&gt; extends ResourceSupport {
      @JsonProperty("_embedded")
      private EmbeddedDto&lt;T&gt; embedded;
  
      public EmbeddedDto&lt;T&gt; getEmbedded() {
          return embedded;
      }
  
      public void setEmbedded(EmbeddedDto&lt;T&gt; embedded) {
          this.embedded = embedded;
      }
  }
  </code></pre>
  
  <p>EmbeddedDto:</p>
  
  <pre><code>public class EmbeddedDto&lt;T&gt; {
      @JsonProperty("dto")
      private List&lt;T&gt; dtoList;
  
      public List&lt;T&gt; getDtoList()
      {
          return dtoList;
      }
  
      public void setDto(List&lt;T&gt; dtoList) {
          this.dtoList = dtoList;
      }
  }
  </code></pre>
  
  <p>Those classes are necessary (i think) because Spring Data returns data in the HAL (<a href="https://en.wikipedia.org/wiki/Hypertext_Application_Language" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Hypertext_Application_Language</a>) format.</p>
  
  <p>Note: Produto must extend ResourceSupport.</p>
  
  <p>Caveats: All collectionResourceRel must be named "dto" and it only works for collections (may be adjusted).</p>
  
  <p>Is this the proper way to do this?</p>
  
  <p>I have googled around and found plenty of examples of doing the server side, but almost nothing on building clients.</p>
  
  <p>Thanks.</p>