Java restful web services with Spring, jersey, JpaRepository, maven, angularjs and oracle

Create Web Project from Maven:

  Open CMD > Type the commend

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} - DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false 

example :
 mvn archetype:generate -DgroupId=com.Webapp -DartifactId=WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


    Import the maven created project into IDE(Eclipse or Intellij)

Project Structure:

Add POM dependencies:

Open pom.xml
   <dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
<version>${jersey.version}</version>
</dependency>

<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>


<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
<version>${spring.integration.version}</version>
</dependency>


<!-- ******* JPA/Hibernate ******** -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- testing -->
<!-- Jersey-client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.3.0</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.0.M1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.M1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>

Go to your POM location and mvn clean install  cmd


App.java


  1. package App;

  2. import org.glassfish.jersey.server.ResourceConfig;

  3. public class App extends ResourceConfig{

  4. public App() {
  5.         register(AppResource.class);
  6. }

  7. }

Database Table structure

CREATE TABLE Table_Name
(
id long(10),
name varchar(255),
address varchar(255)
);

UserEntity.java

  1. package App;

  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6. import javax.xml.bind.annotation.XmlRootElement;


  7. @Entity
  8. @Table(name = "Table_Name", catalog = "", schema = "user")
  9. @XmlRootElement
  10. public class UserEntity {
  11.     @Id
  12.     @Column(name="id")
  13.     private long id;

  14.     @Column(name="name")
  15.     private String name;

  16.     private String address;

  17.     public long getId() {
  18.         return id;
  19.     }

  20.     public void setId(long id) {
  21.         this.id = id;
  22.     }

  23.     public String getName() {
  24.         return name;
  25.     }

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

  29.     public String getAddress() {
  30.         return address;
  31.     }

  32.     public void setAddress(String address) {
  33.         this.address = address;
  34.     }
  35. }

Repository.java

  1. package App;

  2. import org.springframework.data.jpa.repository.JpaRepository;


  3. public interface Repository extends JpaRepository<UserEntity, Long> {
  4.     public UserEntity findById(Long id);
  5. }

Service.java

  1. package App;

  2. import java.util.Collection;


  3. public interface Service {
  4.     public Collection<UserEntity> viewAll();
  5. }

ServiceImplementation.java

  1. package App;

  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;

  4. import java.util.Collection;


  5. @Component
  6. public class ServiceImplementation implements Service{
  7.     @Autowired
  8.     private Repository repository;


  9. @Override
  10. public Collection<UserEntity> viewAll() {
  11. Collection<UserEntity> parameters = (Collection<UserEntity>) repository.findAll();
  12. return parameters;
  13. }
  14. }

AppResource.java

  1. package App;

  2. import javax.ws.rs.Consumes;
  3. import javax.ws.rs.DELETE;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.POST;
  6. import javax.ws.rs.PUT;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.PathParam;
  9. import javax.ws.rs.Produces;
  10. import javax.ws.rs.core.MediaType;
  11. import javax.ws.rs.core.Response;

  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;

  14. import java.util.Collection;

  15. @Component
  16. @Path("/App")
  17. public class AppResource {

  18.     @Autowired
  19.     private Service service;

  20.     public static final String HTTP = "http://";
  21.     public static final String host = "localhost";
  22.     public static final String port = "8040";
  23.     public static final String URI= "/App/read";
  24.     public static final String CREATE = "create";
  25.     public static final String READ = "read";
  26.     public static final String UPDATE = "update";
  27.     public static final String DELETE = "delete";

  28. @GET
  29. @Path("/read")
  30. @Produces({ MediaType.APPLICATION_JSON })
  31. public Response getData() throws Exception {
  32. Collection<UserEntity> parameters;
  33. try {
  34. parameters = service.viewAll();
  35. } catch (Exception e) {
  36. String message = "Error getting all!";
  37. throw new Exception(message + " - " + e.getMessage());
  38. }
  39. String endPoint = HTTP + host + ":" + port + URI + READ;
  40. // 200
  41. Response response = Response.status(Response.Status.OK).entity(parameters).header("Location", endPoint).build();
  42. return response;
  43. }

  44. }

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation