In my last post Setting up a Postgresql database, I showed how to install and setup a simple postgres local database. Now I am going to proceed and create a simple java Spring project that exposes a rest api endpoint allowing to query and fetch data from the database.
The IDE i am using is Eclipse and I will be using Maven for building the project.
To start create a Maven project using the archetype - maven-archetype-webapp.
In Eclipse, I enable Dynamic Web Module Facet in the project's properties and set the compiler level to 1.7 at least.
- Update the pom.xml file to add required dependencies. For our project, we will use spring-webmvc , javax.servlet-api, jackson-core and jackson-databind. These are required to build a rest api endpoing with automatic conversion of java objects to json and vice versa.
- Also add following dependencies to enable persistence with the postgreSQL database - spring-data-jpa, hibernate-entitymanager, postgresql and jta.
- Now that we got all our dependencies added, lets start building the project. I will start by setting up my project to talk to the database via Spring data and Hibernate for JPA.
- In project, I created a package "org.rajesh.learning.UserSpringMVCPostgres.Config" and added a class "DBConfig".
- In that class, I am going to create beans for LocalContainerEntityManagerFactoryBean, DataSource and a PlatformTransactionManager. As you will notice I am going to use java class based configuration for my JPA instead of XML. The class will look like below -
Also I am using two other annotations to enable Spring data JPA capabilities for transaction management and recognizing which package has my JPA repository definitions.
Couple of other important things that i did - In the datasource class and the entityManager, I setup all the required properties for connecting to my postgres database. Also I indicate which package to scan to find my entities.
- Next let me setup the class for my data Model. From my previous tutorial on the database setup Setting up a Postgresql database, you will remember there is a table "users" with colums - firstname, lastname and login.
- So I created a class "User" in a package org.rajesh.learning.UserSpringMVCPostgres.Model as shown below
- Next lets create the Repository that will support the basic CRUD operations. Here is where Spring Data comes into play. Just by adding a CRUDRepository interface, we can get this functionality. That is the power of using a framework like Spring!
I have annotated it with @Repository to indicate this is a repository. You will notice I have not implemented any abstract methods in it. So we will just use the methods that the interface will provide us. For ex: findAll()
- Now that we have done everything to support our java persistence, lets move on to setting up the rest api endpoint. First let me set up the WebApplicationInitializer. I am not going to setup the servlet using the web.xml and will use the WebApplicationInitializer interface instead. I believe this is part of the Servlet 3.0 specification.
- Now lets setup some configuration classes. I will setup a AppConfig class and indicate that the package to scan for generating beans via the @ComponentScan annotation. Also I setup another Config class with @EnableWebMvc annotation. This imports the Spring MVC configuration to the project.
- Now lets setup the Rest Controller class that will expose our end point. For this demo, i will just setup one simple GET method and use the findAll() repository event.
Thats it, my project is setup. Next I built the project using maven - mvn clean install command. Then I deployed the project to my local Tomcat 8 server.
Here are the results -
Cool, we have a simple api setup minus features like authentication. Next I intend to develop this a bit further and then use it in my Salesforce org. I would like to switch over to Salesforce to make some http callouts and explore Apex a bit. Till then, enjoy folks!
Comments
Post a Comment