Microservices are going completely over the enterprise and changed the way people write software within an enterprise ecosystem.

Let build you microservices with msf4j for Auto Mobile.

1) Create msf4j using apache archetype with below command

mvn archetype:generate -DarchetypeGroupId=org.wso2.msf4j -DarchetypeArtifactId=msf4j-microservice -DarchetypeVersion=1.0.0 -DgroupId=org.example -DartifactId=automobile -Dversion=0.1-SNAPSHOT -Dpackage=org.example.service -DserviceClass=AutomobileService

image

2) Open it on IDE respectively  idea and eclipse by going into the directory

mvn idea:idea

mvn eclipse:eclipse

image

3) Change the context to ‘/automobile’ from ‘/service’

image

4) Create new Java class called “Automobile” that will contain the blueprint for our micro services. You can used auto generator build getter and setter and constructor.

public class Automobile {

private String brand;
private String name;
private int engineeSize;
private double price;
}

5) Improve the GET and POST as below

@Path("/automobile")
public class AutomobileService {

private Map<String, Automobile> automobiles = new HashMap<>();

public AutomobileService() {
automobiles.put("toyota", new Automobile("Toyota", "Prado", 2800, 21.3));
}
@GET
@Path("/{brand}")
@Produces("application/json")
public Response get(@PathParam("brand") String brand) {
Automobile automobile = automobiles.get(brand);
return automobile == null ?
Response.status(Response.Status.NOT_FOUND).entity("{\"result\":\"brand not found = " + brand + "\"}")
.build() :
Response.status(Response.Status.OK).entity(automobile).build();
}

@POST
@Consumes("application/json")
public Response addStock(Automobile automobile) {
if(automobiles.get(automobile.getBrand()) != null) {
return Response.status(Response.Status.CONFLICT).build();
}
automobiles.put(automobile.getBrand(), automobile);
return Response.status(Response.Status.OK).
entity("{\"result\":\"Updated the automobile with brand = " + automobile.getBrand() + "\"}").build();
}


6) Build it from maven

maven clean install


7) Run it and test

java -jar target/automobile-0.1-SNAPSHOT.jar


8) Go to Postman and test GET and POST

image

POST request

image

Then Check the post has recorded value

image

Now, you can try DELETE and PUT from you self.

0

Add a comment

I am
I am
Archives
Total Pageviews
Total Pageviews
2 0 5 5 9 0 3
Categories
Categories
Loading