Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact

Last Updated: March 26, 2021

This section shows how to create a Mature REST Microservice API’s in no-time.

Steps:

  1. Create Maven project such as the following:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jalalkiswani.examples</groupId>
	<artifactId>smart-cloud-microservice-mature-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<smart-cloud.version>4.0.8</smart-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-web</artifactId>
			<version>${smart-cloud.version}</version>
		</dependency>

		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-data</artifactId>
			<version>${smart-cloud.version}</version>
		</dependency>

		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-standalone</artifactId>
			<version>${smart-cloud.version}</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

If you are using Eclipse, be sure to refresh your project (select your project→ right click→ Maven→ Update Project)

  1. Create Model class at src/main/java/com/app/person.Model.java, such as the following:

package com.app.person;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="person")
public class Model implements Serializable {

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

	@Column(name="national_id")
	private String nationalId;

	@Column(name="name")
	private String name;

	@Column(name="email")
	private String email;

	@Column(name="address")
	private String address;


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

	public Integer getId(){
	 return this.id;
	}

	public void setNationalId(String nationalId){
	  this.nationalId=nationalId;
	}

	public String getNationalId(){
	 return this.nationalId;
	}

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

	public String getName(){
	 return this.name;
	}

	public void setEmail(String email){
	  this.email=email;
	}

	public String getEmail(){
	 return this.email;
	}

	public void setAddress(String address){
	  this.address=address;
	}

	public String getAddress(){
	 return this.address;
	}


	@Override
	public boolean equals(Object obj) {
	  if (obj == null) {
		return false;
	  }
	  return this.getId().equals(((Model) obj).getId());
	}

	@Override
	public int hashCode() {
	  if(this.id==null) {
	    return toString().hashCode();
	   }
	  return this.id.hashCode();
	}

	@Override
	public String toString(){
	  StringBuffer buf=new StringBuffer();
	  buf.append(this.id).append(",");
	  buf.append(this.nationalId).append(",");
	  buf.append(this.name).append(",");
	  buf.append(this.email).append(",");
	  buf.append(this.address).append(",");
	  return buf.toString();
	}
}
  1. Create Service class as REST service at src/main/java/com/app/person/Service.java, such as the following:

package com.app.person;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.jk.db.dataaccess.orm.JKObjectDataAccess;
import com.jk.db.datasource.JKDataAccessFactory;
import com.jk.util.JK;

@Path("/persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Service {
	JKObjectDataAccess da = JKDataAccessFactory.getObjectDataAccess();

	@GET
	public Response getAll() {
		List<Model> list = da.getList(Model.class);
		return Response.status(200).entity(list).build();
	}

	@GET
	@Path("/{id}")
	public Response find(@PathParam("id") int id) {
		Model person = da.find(Model.class, id);
		if (person == null) {
			return Response.status(404).build();
		}
		return Response.status(200).entity(person).build();
	}

	@POST
	public Response insert(Model person) {
		da.insert(person);
		return Response.status(201).entity(person).build();
	}

	@PUT
	@Path("/{id}")
	public Response update(@PathParam("id") int id, Model person) {
		JK.fixMe("Validate againest the provided id, and check if exists");
		da.update(person);
		return Response.status(201).entity(person).build();
	}

	@DELETE
	@Path("/{id}")
	public Response delete(@PathParam("id") int id) {
		da.delete(Model.class, id);
		return Response.status(201).build();
	}

}
  1. Create App class at src/main/java/com/app/App.java for quick testing with the following contents:

package com.app;

import javax.ws.rs.ApplicationPath;

import com.jk.web.embedded.JKWebApplication;
import com.jk.web.services.server.JKServiceConfig;

@ApplicationPath("app")
public class App extends JKServiceConfig{

	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		JKWebApplication.run(8080,false);
	}

}

Run your App class, you should see something like this in your console:

mature service

The available end-points are:

Now, you can test the Microservice with your preferred API client such as Talend or PostMan.

To call the this Microservice from Java client, you can follow this - Create Mature Microservice Client

Full example source-code can be found at https://github.com/smartapi-com/smart-cloud-microservice-mature


Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact