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
Newsletter
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

  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy
  • OwlVigil Open-Sources Its Go SDK for Enterprise AI
  • 7 Swift Features That Will Let You Delete Code Tomorrow
  • Persisting Cookies Across JAX-WS Service Calls Using a Custom SOAP Handler

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 09, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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