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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Madhuka  Udantha user avatar by
Madhuka Udantha
CORE ·
Aug. 09, 18 · Tutorial
Like (7)
Save
Tweet
Share
5.67K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Automated Performance Testing With ArgoCD and Iter8
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • TDD: From Katas to Production Code
  • What Is Browser Sandboxing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: