BAEL-1354 Guide to microservice with meecrowave (#4612)
This commit is contained in:
parent
30b439458e
commit
6be948a05a
|
@ -0,0 +1,57 @@
|
|||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-meecrowave</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<name>apache-meecrowave</name>
|
||||
<description>A sample REST API application with Meecrowave</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-core</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-jpa</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.10.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-junit</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-maven-plugin</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
public class Article {
|
||||
private String name;
|
||||
private String author;
|
||||
|
||||
public Article() {
|
||||
}
|
||||
|
||||
public Article(String name, String author) {
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
@RequestScoped
|
||||
@Path("article")
|
||||
public class ArticleEndpoints {
|
||||
|
||||
@Inject
|
||||
ArticleService articleService;
|
||||
|
||||
@GET
|
||||
public Response getArticle() {
|
||||
return Response.ok()
|
||||
.entity(new Article("name", "author"))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response createArticle(Article article) {
|
||||
return Response.status(Status.CREATED)
|
||||
.entity(articleService.createArticle(article))
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ArticleService {
|
||||
public Article createArticle(Article article) {
|
||||
return article;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import org.apache.meecrowave.Meecrowave;
|
||||
|
||||
public class Server {
|
||||
public static void main(String[] args) {
|
||||
final Meecrowave.Builder builder = new Meecrowave.Builder();
|
||||
builder.setScanningPackageIncludes("com.baeldung.meecrowave");
|
||||
builder.setJaxrsMapping("/api/*");
|
||||
builder.setJsonpPrettify(true);
|
||||
|
||||
try (Meecrowave meecrowave = new Meecrowave(builder)) {
|
||||
meecrowave.bake().await();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.meecrowave.Meecrowave;
|
||||
import org.apache.meecrowave.junit.MonoMeecrowave;
|
||||
import org.apache.meecrowave.testing.ConfigurationInject;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
@RunWith(MonoMeecrowave.Runner.class)
|
||||
public class ArticleEndpointsTest {
|
||||
|
||||
@ConfigurationInject
|
||||
private Meecrowave.Builder config;
|
||||
private static OkHttpClient client;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
client = new OkHttpClient();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRetunedArticle_thenCorrect() throws IOException {
|
||||
final String base = "http://localhost:"+config.getHttpPort();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(base+"/article")
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
assertEquals(200, response.code());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-meecrowave</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<name>apache-meecrowave</name>
|
||||
<description>A sample REST API application with Meecrowave</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-core</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-jpa</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.10.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-junit</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.meecrowave</groupId>
|
||||
<artifactId>meecrowave-maven-plugin</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
public class Article {
|
||||
private String name;
|
||||
private String author;
|
||||
|
||||
public Article() {
|
||||
}
|
||||
|
||||
public Article(String name, String author) {
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
@RequestScoped
|
||||
@Path("article")
|
||||
public class ArticleEndpoints {
|
||||
|
||||
@Inject
|
||||
ArticleService articleService;
|
||||
|
||||
@GET
|
||||
public Response getArticle() {
|
||||
return Response.ok()
|
||||
.entity(new Article("name", "author"))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response createArticle(Article article) {
|
||||
return Response.status(Status.CREATED)
|
||||
.entity(articleService.createArticle(article))
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ArticleService {
|
||||
public Article createArticle(Article article) {
|
||||
return article;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
import org.apache.meecrowave.Meecrowave;
|
||||
|
||||
public class Server {
|
||||
public static void main(String[] args) {
|
||||
final Meecrowave.Builder builder = new Meecrowave.Builder();
|
||||
builder.setScanningPackageIncludes("com.baeldung.meecrowave");
|
||||
builder.setJaxrsMapping("/api/*");
|
||||
builder.setJsonpPrettify(true);
|
||||
|
||||
try (Meecrowave meecrowave = new Meecrowave(builder)) {
|
||||
meecrowave.bake().await();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.meecrowave;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.meecrowave.Meecrowave;
|
||||
import org.apache.meecrowave.junit.MonoMeecrowave;
|
||||
import org.apache.meecrowave.testing.ConfigurationInject;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
@RunWith(MonoMeecrowave.Runner.class)
|
||||
public class ArticleEndpointsTest {
|
||||
|
||||
@ConfigurationInject
|
||||
private Meecrowave.Builder config;
|
||||
private static OkHttpClient client;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
client = new OkHttpClient();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRetunedArticle_thenCorrect() throws IOException {
|
||||
final String base = "http://localhost:"+config.getHttpPort();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(base+"/article")
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
assertEquals(200, response.code());
|
||||
}
|
||||
}
|
5
pom.xml
5
pom.xml
|
@ -149,7 +149,7 @@
|
|||
<module>spring-boot-persistence</module>
|
||||
<module>spring-boot-security</module>
|
||||
<module>spring-boot-mvc</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud</module>
|
||||
<module>spring-core</module>
|
||||
|
@ -267,11 +267,12 @@
|
|||
<module>java-spi</module>
|
||||
<module>performance-tests</module>
|
||||
<module>twilio</module>
|
||||
<module>spring-boot-ctx-fluent</module>
|
||||
<module>spring-boot-ctx-fluent</module>
|
||||
<module>java-ee-8-security-api</module>
|
||||
<module>spring-webflux-amqp</module>
|
||||
<module>antlr</module>
|
||||
<module>maven-archetype</module>
|
||||
<module>apache-meecrowave</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
Loading…
Reference in New Issue