Skip to content

Commit 6be948a

Browse files
dhruba619pivovarit
authored andcommitted
BAEL-1354 Guide to microservice with meecrowave (eugenp#4612)
1 parent 30b4394 commit 6be948a

13 files changed

Lines changed: 375 additions & 2 deletions

File tree

apache-meecrowave/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-meecrowave</artifactId>
6+
<version>0.0.1</version>
7+
<name>apache-meecrowave</name>
8+
<description>A sample REST API application with Meecrowave</description>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
16+
<dependency>
17+
<groupId>org.apache.meecrowave</groupId>
18+
<artifactId>meecrowave-core</artifactId>
19+
<version>1.2.1</version>
20+
</dependency>
21+
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
22+
<dependency>
23+
<groupId>org.apache.meecrowave</groupId>
24+
<artifactId>meecrowave-jpa</artifactId>
25+
<version>1.2.1</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.squareup.okhttp3</groupId>
30+
<artifactId>okhttp</artifactId>
31+
<version>3.10.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.meecrowave</groupId>
35+
<artifactId>meecrowave-junit</artifactId>
36+
<version>1.2.0</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/junit/junit -->
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.10</version>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.meecrowave</groupId>
52+
<artifactId>meecrowave-maven-plugin</artifactId>
53+
<version>1.2.1</version>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.meecrowave;
2+
3+
public class Article {
4+
private String name;
5+
private String author;
6+
7+
public Article() {
8+
}
9+
10+
public Article(String name, String author) {
11+
this.author = author;
12+
this.name = name;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public String getAuthor() {
24+
return author;
25+
}
26+
27+
public void setAuthor(String author) {
28+
this.author = author;
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.meecrowave;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Inject;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.POST;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.core.Response;
9+
import javax.ws.rs.core.Response.Status;
10+
11+
@RequestScoped
12+
@Path("article")
13+
public class ArticleEndpoints {
14+
15+
@Inject
16+
ArticleService articleService;
17+
18+
@GET
19+
public Response getArticle() {
20+
return Response.ok()
21+
.entity(new Article("name", "author"))
22+
.build();
23+
24+
}
25+
26+
@POST
27+
public Response createArticle(Article article) {
28+
return Response.status(Status.CREATED)
29+
.entity(articleService.createArticle(article))
30+
.build();
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.meecrowave;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
5+
@ApplicationScoped
6+
public class ArticleService {
7+
public Article createArticle(Article article) {
8+
return article;
9+
}
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.meecrowave;
2+
3+
import org.apache.meecrowave.Meecrowave;
4+
5+
public class Server {
6+
public static void main(String[] args) {
7+
final Meecrowave.Builder builder = new Meecrowave.Builder();
8+
builder.setScanningPackageIncludes("com.baeldung.meecrowave");
9+
builder.setJaxrsMapping("/api/*");
10+
builder.setJsonpPrettify(true);
11+
12+
try (Meecrowave meecrowave = new Meecrowave(builder)) {
13+
meecrowave.bake().await();
14+
}
15+
}
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.meecrowave;
2+
3+
4+
import static org.junit.Assert.assertEquals;
5+
6+
import java.io.IOException;
7+
8+
import org.apache.meecrowave.Meecrowave;
9+
import org.apache.meecrowave.junit.MonoMeecrowave;
10+
import org.apache.meecrowave.testing.ConfigurationInject;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
import okhttp3.OkHttpClient;
16+
import okhttp3.Request;
17+
import okhttp3.Response;
18+
19+
@RunWith(MonoMeecrowave.Runner.class)
20+
public class ArticleEndpointsTest {
21+
22+
@ConfigurationInject
23+
private Meecrowave.Builder config;
24+
private static OkHttpClient client;
25+
26+
@BeforeClass
27+
public static void setup() {
28+
client = new OkHttpClient();
29+
}
30+
31+
@Test
32+
public void whenRetunedArticle_thenCorrect() throws IOException {
33+
final String base = "http://localhost:"+config.getHttpPort();
34+
35+
Request request = new Request.Builder()
36+
.url(base+"/article")
37+
.build();
38+
Response response = client.newCall(request).execute();
39+
assertEquals(200, response.code());
40+
}
41+
}

meecrowave/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-meecrowave</artifactId>
6+
<version>0.0.1</version>
7+
<name>apache-meecrowave</name>
8+
<description>A sample REST API application with Meecrowave</description>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-core -->
16+
<dependency>
17+
<groupId>org.apache.meecrowave</groupId>
18+
<artifactId>meecrowave-core</artifactId>
19+
<version>1.2.1</version>
20+
</dependency>
21+
<!-- https://mvnrepository.com/artifact/org.apache.meecrowave/meecrowave-jpa -->
22+
<dependency>
23+
<groupId>org.apache.meecrowave</groupId>
24+
<artifactId>meecrowave-jpa</artifactId>
25+
<version>1.2.1</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.squareup.okhttp3</groupId>
30+
<artifactId>okhttp</artifactId>
31+
<version>3.10.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.meecrowave</groupId>
35+
<artifactId>meecrowave-junit</artifactId>
36+
<version>1.2.0</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/junit/junit -->
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.10</version>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.meecrowave</groupId>
52+
<artifactId>meecrowave-maven-plugin</artifactId>
53+
<version>1.2.1</version>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.meecrowave;
2+
3+
public class Article {
4+
private String name;
5+
private String author;
6+
7+
public Article() {
8+
}
9+
10+
public Article(String name, String author) {
11+
this.author = author;
12+
this.name = name;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public String getAuthor() {
24+
return author;
25+
}
26+
27+
public void setAuthor(String author) {
28+
this.author = author;
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.meecrowave;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Inject;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.POST;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.core.Response;
9+
import javax.ws.rs.core.Response.Status;
10+
11+
@RequestScoped
12+
@Path("article")
13+
public class ArticleEndpoints {
14+
15+
@Inject
16+
ArticleService articleService;
17+
18+
@GET
19+
public Response getArticle() {
20+
return Response.ok()
21+
.entity(new Article("name", "author"))
22+
.build();
23+
24+
}
25+
26+
@POST
27+
public Response createArticle(Article article) {
28+
return Response.status(Status.CREATED)
29+
.entity(articleService.createArticle(article))
30+
.build();
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.meecrowave;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
5+
@ApplicationScoped
6+
public class ArticleService {
7+
public Article createArticle(Article article) {
8+
return article;
9+
}
10+
}

0 commit comments

Comments
 (0)