DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How to Create a Microservice Architecture With Java
  • Techniques You Should Know as a Kafka Streams Developer
  • Legacy Code Refactoring: Tips, Steps, and Best Practices

Trending

  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • A Hands-On ABAP RESTful Programming Model Guide
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Offline-First Patch Management for 10,000 Edge Nodes: A Practical Architecture That Scales
  1. DZone
  2. Data Engineering
  3. Data
  4. Writing Microservices With msf4j (Microservices Framework for Java)

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.

By 
Madhuka  Udantha user avatar
Madhuka Udantha
·
Aug. 09, 18 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
6.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

microservice Java (programming language) Framework

Published at DZone with permission of Madhuka Udantha. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How to Create a Microservice Architecture With Java
  • Techniques You Should Know as a Kafka Streams Developer
  • Legacy Code Refactoring: Tips, Steps, and Best Practices

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook