Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Writing Microservices With msf4j (Microservices Framework for Java)

DZone's Guide to

Writing Microservices With msf4j (Microservices Framework for Java)

Learn how to write and test microservices with Java in the Microservices Framework for Java, also known as msf4j, in this tutorial.

· Microservices Zone ·
Free Resource

Deploy commerce faster and keep pace with the demands of your customers and executives. Read this blueprint to learn how to create your own microservices-based commerce foundation so you can quickly move onto building innovative and unique shopping experiences for your customers.

Microservices are taking over the enterprise and changing the way people write software within an enterprise ecosystem.

Let's build microservices with msf4j (Microservices Framework for Java) for Auto Mobile.

1) Create msf4j using Apache Archetype with the 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 title

2) Open it in your IDEs, IDEA and Eclipse, respectively, by going into the directory:

 mvn idea:idea 

 mvn eclipse:eclipse 

Image title

3) Change the context from "/service" to "/automobile."

Image title

4) Create a new Java class called “Automobile” that will contain the blueprint for our microservices. You can use the auto-generator build getter, 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 with Maven:

 maven clean install 

7) Run it and test it.

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

8) Go to Postman and test GET and POST:

Image title

POST request:

Image title

Then check that the post has the recorded value:

Image title

Now, you can try DELETE and PUT by yourself.

A commerce architecture built by microservices allows for agile development, shorter release cycles, and faster time-to-market. Read this helpful guide to learn more about how to structure your commerce architecture with microservices.

Topics:
microservices ,tutorial ,msf4j ,software architecture ,java ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}