Monday, November 23, 2015

Spring boot and @RepositoryRestResource example with Hibernate and no Controller/Service layer and HATEOS

Hi,

Spring boot part two.  

Problem taken
In this blog I am integrating Spring boot with Hibernate.  In this I will demonstrate how easy it is to do following things:

  • Connect with Hibernate
  • Create Entity manager or datasource
  • No need of Controller/Service layer
  • Directly expose Repository as Rest path and access entity
  • How to check entity links feature
  • With only 3 to 4 files :)


Technology stack

  • Java8
  • Spring boot 1.3.x
  • Spring 4.2.3
  • Hibernate 4.x
  • Apache Maven 3.3.x
  • IDE - IntelliJ 14.x Community Edition

Solution
Just write few lines of code and you are done in generating Json.  How?  Let's see.

I just added pom.xml, one controller, one model, one repository and Application.java which will be starting point for Spring boot.  Since explanation wise there is not much I am directly coming to the code.

File pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring</groupId>
    <artifactId>boot</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <start-class>com.spring.experiments.Application</start-class>
        <hibernate.version>4.2.11.Final</hibernate.version>
        <mysql.connector.version>5.1.36</mysql.connector.version>

        <!-- Logging -->        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-repository</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
            <version>2.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-core</artifactId>
            <version>2.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.182</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


File NotebookRepository.java
package com.spring.experiments.repository;

import com.spring.experiments.model.Notebook;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.repository.annotation.RestResource;

import java.util.List;

/** * Created by shaiverm on 23-Nov-2015 */@RepositoryRestResource(collectionResourceRel = "notebook", path = "notebook")
public interface NotebookRepository extends PagingAndSortingRepository<Notebook, Long> {

    List<Notebook> findByName(@Param("name") String name);

}


File Notebook.java
package com.spring.experiments.model;

import javax.persistence.*;
import java.sql.Date;

/** * Created by shaiverm on 17-Nov-2015 */@Entity@Table (name = "notebook")
public class Notebook {

    @Id    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    private String name, description, tags, author;

    /*@Column(name = "created_on")    private Date createdOn;*/
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    /*public Date getCreatedOn() {        return createdOn;    }
    public void setCreatedOn(Date createdOn) {        this.createdOn = createdOn;    }*/
    @Override    public String
    toString() {
        return "Notebook{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", tags='" + tags + '\'' +
                ", author='" + author + '\'' +
                //", createdOn=" + createdOn +                '}';
    }
}


File Application.java
package com.spring.experiments;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.out.println("Spring Boot begins...");
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
        System.out.println("Spring Boot started...");
    }

}

File application.properties
spring.datasource.url=jdbc:mysql://localhost/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver

Output Sample (Observe links with _links)
Url (Searching a record) - http://localhost:8080/notebook/search/findByName?name=Java8
{
}

Url (List of records) - http://localhost:8080/notebook/
{
}


Url (Check entity details) - http://localhost:8080/profile/notebook
{
  • alps:
    {
    • version"1.0",
    • descriptors:
      [
      • {
        • id"notebook-representation",
        • descriptors:
          [
          • {
            • name"name",
            • type"SEMANTIC"
            },
          • {
            • name"description",
            • type"SEMANTIC"
            },
          • {
            • name"tags",
            • type"SEMANTIC"
            },
          • {
            • name"author",
            • type"SEMANTIC"
            }
          ]
        },
      • {
        • id"get-notebook",
        • name"notebook",
        • type"SAFE",
        • rt"#notebook-representation",
        • descriptors:
          [
          • {
            • name"page",
            • doc:
              {
              • value"The page to return.",
              • format"TEXT"
              },
            • type"SEMANTIC"
            },
          • {
            • name"size",
            • doc:
              {
              • value"The size of the page to return.",
              • format"TEXT"
              },
            • type"SEMANTIC"
            },
          • {
            • name"sort",
            • doc:
              {
              • value"The sorting criteria to use to calculate the content of the page.",
              • format"TEXT"
              },
            • type"SEMANTIC"
            }
          ]
        },
      • {
        • id"create-notebook",
        • name"notebook",
        • type"UNSAFE",
        • rt"#notebook-representation"
        },
      • {
        • id"delete-notebook",
        • name"notebook",
        • type"IDEMPOTENT",
        • rt"#notebook-representation"
        },
      • {
        • id"patch-notebook",
        • name"notebook",
        • type"UNSAFE",
        • rt"#notebook-representation"
        },
      • {
        • id"get-notebook",
        • name"notebook",
        • type"SAFE",
        • rt"#notebook-representation"
        },
      • {
        • id"update-notebook",
        • name"notebook",
        • type"IDEMPOTENT",
        • rt"#notebook-representation"
        },
      • {
        • name"findByName",
        • type"SAFE",
        • descriptors:
          [
          • {
            • name"name",
            • type"SEMANTIC"
            }
          ]
        }
      ]
    }
}



How to Run

  • mvn spring-boot:run
              or
  • Use IDE by running Application.java
              or
  • Make this application as webapp and deploy in Tomcat/Jetty or any web server.


Code at Github
I have posted my same code at Github.  Please visit below link to access it.

You can reach me for any queries at my email address.

Thanks
Shailendra

No comments:

Post a Comment