BAEL-1388 Introduction to Java MSF4J Microservices (#4192)
* BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * Updates Thymeleaf version to 3.0.9.RELEASE * Added msf4j projects * updated msf4j project folder * fixed issue with spring-thymeleaf/pom.xml * Removed depedency-reduced-pom.xml * Whitespacing fix
This commit is contained in:
parent
2efdeed00f
commit
a023f014d0
|
@ -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>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
pom.xml
1
pom.xml
|
@ -102,6 +102,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>
|
||||
|
|
|
@ -173,4 +173,4 @@
|
|||
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue