Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions msf4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-service</artifactId>
<version>2.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung.msf4j</groupId>
<artifactId>msf4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WSO2 MSF4J Microservice</name>

<properties>
<microservice.mainClass>com.baeldung.msf4j.msf4jintro.Application</microservice.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-spring</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-mustache-template</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>

</project>
11 changes: 11 additions & 0 deletions msf4j/src/main/java/com/baeldung/msf4j/msf4japi/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.msf4j.msf4japi;

import org.wso2.msf4j.MicroservicesRunner;

public class Application {
public static void main(String[] args) {
new MicroservicesRunner()
.deploy(new MenuService())
.start();
}
}
20 changes: 20 additions & 0 deletions msf4j/src/main/java/com/baeldung/msf4j/msf4japi/Meal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.msf4j.msf4japi;

public class Meal {
private String name;
private Float price;

public Meal(String name, Float price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public Float getPrice() {
return price;
}

}
78 changes: 78 additions & 0 deletions msf4j/src/main/java/com/baeldung/msf4j/msf4japi/MenuService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.baeldung.msf4j.msf4japi;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;

@Path("/menu")
public class MenuService {

private List<Meal> meals = new ArrayList<Meal>();

public MenuService() {
meals.add(new Meal("Java beans",42.0f));
}

@GET
@Path("/")
@Produces({ "application/json" })
public Response index() {
return Response.ok()
.entity(meals)
.build();
}

@GET
@Path("/{id}")
@Produces({ "application/json" })
public Response meal(@PathParam("id") int id) {
return Response.ok()
.entity(meals.get(id))
.build();
}


@POST
@Path("/")
@Consumes("application/json")
@Produces({ "application/json" })
public Response create(Meal meal) {
meals.add(meal);
return Response.ok()
.entity(meal)
.build();
}

@PUT
@Path("/{id}")
@Consumes("application/json")
@Produces({ "application/json" })
public Response update(@PathParam("id") int id, Meal meal) {
meals.set(id, meal);
return Response.ok()
.entity(meal)
.build();
}

@DELETE
@Path("/{id}")
@Produces({ "application/json" })
public Response delete(@PathParam("id") int id) {
Meal meal = meals.get(id);
meals.remove(id);
return Response.ok()
.entity(meal)
.build();
}
}
11 changes: 11 additions & 0 deletions msf4j/src/main/java/com/baeldung/msf4j/msf4jintro/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.msf4j.msf4jintro;

import org.wso2.msf4j.MicroservicesRunner;

public class Application {
public static void main(String[] args) {
new MicroservicesRunner()
.deploy(new SimpleService())
.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.msf4j.msf4jintro;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/")
public class SimpleService {

@GET
public String index() {
return "Default content";
}

@GET
@Path("/say/{name}")
public String say(@PathParam("name") String name) {
return "Hello " + name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.msf4j.msf4jspring;

import org.wso2.msf4j.spring.MSF4JSpringApplication;

public class Application {

public static void main(String[] args) {
MSF4JSpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.msf4j.msf4jspring.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.wso2.msf4j.spring.transport.HTTPTransportConfig;

@Configuration
public class PortConfiguration {

@Bean
public HTTPTransportConfig http() {
return new HTTPTransportConfig(9090);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.msf4j.msf4jspring.domain;

public class Meal {
private String name;
private Float price;

public Meal(String name, Float price) {
this.name = name;
this.price = price;
}

public String getName() {
return name;
}

public Float getPrice() {
return price;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.msf4j.msf4jspring.repositories;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.baeldung.msf4j.msf4jspring.domain.Meal;

@Component
public class MealRepository {

private List<Meal> meals = new ArrayList<Meal>();

public MealRepository() {
meals.add(new Meal("Salad", 4.2f));
meals.add(new Meal("Litre of cola", 2.99f));
}

public void create(Meal meal) {
meals.add(meal);
}

public void remove(Meal meal) {
meals.remove(meal);
}

public Meal find(int id) {
return meals.get(id);
}

public List<Meal> findAll() {
return meals;
}

public void update(int id, Meal meal) {
meals.set(id, meal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.baeldung.msf4j.msf4jspring.resources;

import java.util.Collections;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wso2.msf4j.template.MustacheTemplateEngine;

import com.baeldung.msf4j.msf4jspring.services.MealService;

@Component
@Path("/meal")
public class MealResource {

@Autowired
private MealService mealService;

@GET
@Path("/")
public Response all() {
Map map = Collections.singletonMap("meals", mealService.findAll());
String html = MustacheTemplateEngine.instance()
.render("meals.mustache", map);
return Response.ok()
.type(MediaType.TEXT_HTML)
.entity(html)
.build();
}

@GET
@Path("/{id}")
@Produces({ "text/xml" })
public Response meal(@PathParam("id") int id) {
return Response.ok()
.entity(mealService.find(id))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.msf4j.msf4jspring.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baeldung.msf4j.msf4jspring.domain.Meal;
import com.baeldung.msf4j.msf4jspring.repositories.MealRepository;

@Service
public class MealService {
@Autowired
private MealRepository mealRepository;

public Meal find(int id) {
return mealRepository.find(id);
}

public List<Meal> findAll() {
return mealRepository.findAll();
}

public void create(Meal meal) {
mealRepository.create(meal);
}
}
Empty file.
13 changes: 13 additions & 0 deletions msf4j/src/main/resources/templates/meals.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<head>
<title>Meals</title>
</head>
<body>
<div>
<h1>Today's Meals</h1>
{{#meals}}
<div>{{name}}: {{price}}$ </div>
{{/meals}}
</div>
</body>
</html>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<module>metrics</module>
<module>maven</module>
<module>mesos-marathon</module>
<module>msf4j</module>
<module>testing-modules/mockito</module>
<module>testing-modules/mockito-2</module>
<module>testing-modules/mocks</module>
Expand Down
2 changes: 1 addition & 1 deletion spring-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
</properties>

</project>
</project>