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

Last Updated: March 26, 2021

This section shows how to create a simple REST Microservice service based on Java Jackson and Jersy API’s in no-time.

Steps:

  1. Create Maven project with the following pom.xml:

<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-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-standalone</artifactId>
			<version>${smart-cloud.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-web</artifactId>
			<version>${smart-cloud.version}</version>
		</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 Person class as a model class at src/main/java/com/app/model/Person.java such as the following:

package com.app.model;

public class Person {

	private String name;

	private int age;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
  1. Create ExampleService as sREST service at src/main/java/com/app/service/ExampleService.java, such as the following:

package com.app.service;

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

import com.app.model.Person;

@Path("/example")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ExampleService {

	@GET
	@Path("hello")
	public String sayHello() {
		return "Hello from uncle Jalal";
	}

	@GET
	@Path("/hello/{name}")
	public String sayHelloWithPathParam(@PathParam(value = "name") String name) {
		return "Hello, " + name;
	}

	@POST
	@Path("/hello")
	public String sayHelloWithBody(Person p) {
		return "Hello, " + p.getName() + ", your age is: " + p.getAge();
	}
}
  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:

console
output
Figure 1. Navigate to http://localhost:8080/app/example/hello , you should see something like this:

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 Simple Microservice Client

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


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