JAVA-13855 Create new microservices-modules (#12612)

This commit is contained in:
anuragkumawat
2022-08-20 14:20:24 +05:30
committed by GitHub
parent 14c998c3ac
commit 6bc1d484ca
95 changed files with 661 additions and 639 deletions
@@ -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 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -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>