title: How to process insert requests in Spring Data before adding them to MongoDB?
body: <p>after following the very useful tutorial at <a href="https://spring.io/guides/gs/accessing-mongodb-data-rest/" rel="nofollow">https://spring.io/guides/gs/accessing-mongodb-data-rest/</a> , I'm trying to create a link shortener app. I've coded an URL class (with <code>id</code>, <code>longURL</code> and <code>hash</code> attributes) and URLRepository class (for now, with just the <code>findByHash</code> method as shown in the demo). Please note that by "hash" I'm referring to a random short string generated by a method yet-to-be-implemented, not an actual hashing function like MD5. Just calls to a independent <code>generateHash(String URL)</code> for example</p>
  
  <p>I'm able to add new entries into the database by using the REST interface provided by Spring, as well as fetching them. However I'd like to have the application generate the hash itself and making some checks and processing before storing it - Mainly checking that the URL is not already stored, in which case it will just return the existing hash.</p>
  
  <p>I suppose that I'd have to extend <code>SimpleMongoRepository</code>'s  <code>&lt;S extends T&gt; List&lt;S&gt; save(Iterable&lt;S&gt; entites);</code> method (As extended by <code>MongoRepository</code>) under my <code>URLRepository</code> class, but I'm not really sure how to do that. I also saw the <code>insert</code> method and I don't know which one I should be using</p>
  
  <p>URL.java</p>
  
  <pre><code>public class URL {
  
      @Id private String id;
  
      private String longURL;
      private String hash;
  
      public String getLongURL() {
          return longURL;
      }
  
      public void setLongURL(String longURL) {
          this.longURL = longURL;
      }
  
      public String getHash() {
          return hash;
      }
  
      public void setHash(String Hash) {
          this.hash = Hash;
      }
  
  }
  </code></pre>
  
  <p>URLRepository.java</p>
  
  <pre><code>import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.data.repository.query.Param;
  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
  import org.springframework.util.Assert;
  
  @RepositoryRestResource(collectionResourceRel = "urls", path = "urls")
  public interface URLRepository extends MongoRepository&lt;URL, String&gt; {
  
      List&lt;URL&gt; findByHash(@Param("hash") String hash);
  
  }
  </code></pre>