Skip to content

Commit e5076f9

Browse files
BAEL-8301 - How to Consume REST API in Quarkus (#17416)
* BAEL-7255 - Implementing GraphQL Mutation without Returning Data * BAEL-7615 - Implement the Builder Pattern in Java 8 * BAEL-7615 - Implement the Builder Pattern in Java 8 * BAEL-7615 - review * BAEL-7780 - A Guide to Micrometer in Quarkus * fix build * fix build * editor review * editor review * fix build * typo * Revert "typo" This reverts commit fabb043. * typo * add version * add version * BAEL-8091 - Getting started with MongoDB and Quarkus * BAEL-8301 - How to Consume REST API in Quarkus * BAEL-7615 - Implement the Builder Pattern in Java 8 * BAEL-7615 - review * BAEL-7780 - A Guide to Micrometer in Quarkus * fix build * fix build * editor review * editor review * typo * Revert "typo" This reverts commit fabb043. * typo * BAEL-8301 - How to Consume REST API in Quarkus * BAEL-8301 - How to Consume REST API in Quarkus * BAEL-8301 - How to Consume REST API in Quarkus * BAEL-8301 - How to Consume REST API in Quarkus * comment module * fix build * downgrade version * downgrade version * format & added tests * format 2 * format 4
1 parent c96a93d commit e5076f9

File tree

17 files changed

+796
-0
lines changed

17 files changed

+796
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>consume-api</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
8+
<parent>
9+
<groupId>com.baeldung.quarkus</groupId>
10+
<artifactId>consume-rest-api</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<properties>
15+
<compiler-plugin.version>3.13.0</compiler-plugin.version>
16+
<maven.compiler.release>17</maven.compiler.release>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19+
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
20+
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
21+
<quarkus.platform.version>3.12.3</quarkus.platform.version>
22+
<skipITs>true</skipITs>
23+
<surefire-plugin.version>3.2.5</surefire-plugin.version>
24+
<junit-jupiter.version>5.7.2</junit-jupiter.version>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>${quarkus.platform.group-id}</groupId>
31+
<artifactId>${quarkus.platform.artifact-id}</artifactId>
32+
<version>${quarkus.platform.version}</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>io.quarkus</groupId>
42+
<artifactId>quarkus-rest</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.quarkus</groupId>
46+
<artifactId>quarkus-rest-client</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.quarkus</groupId>
50+
<artifactId>quarkus-rest-client-jackson</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.quarkus</groupId>
54+
<artifactId>quarkus-arc</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.quarkus</groupId>
58+
<artifactId>quarkus-junit5</artifactId>
59+
<version>${quarkus.platform.version}</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>io.rest-assured</groupId>
64+
<artifactId>rest-assured</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>io.quarkus</groupId>
69+
<artifactId>quarkus-rest-jackson</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.wiremock</groupId>
73+
<artifactId>wiremock</artifactId>
74+
<version>3.9.1</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<!-- <dependency>-->
78+
<!-- <groupId>org.jboss.resteasy</groupId>-->
79+
<!-- <artifactId>resteasy-client</artifactId>-->
80+
<!-- </dependency>-->
81+
<!-- <dependency>-->
82+
<!-- <groupId>io.quarkus</groupId>-->
83+
<!-- <artifactId>quarkus-resteasy-jackson</artifactId>-->
84+
<!-- </dependency>-->
85+
</dependencies>
86+
87+
<build>
88+
<plugins>
89+
<plugin>
90+
<groupId>${quarkus.platform.group-id}</groupId>
91+
<artifactId>quarkus-maven-plugin</artifactId>
92+
<version>${quarkus.platform.version}</version>
93+
<extensions>true</extensions>
94+
<executions>
95+
<execution>
96+
<goals>
97+
<goal>build</goal>
98+
<goal>generate-code</goal>
99+
<goal>generate-code-tests</goal>
100+
<goal>native-image-agent</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
<plugin>
106+
<artifactId>maven-compiler-plugin</artifactId>
107+
<version>${compiler-plugin.version}</version>
108+
<configuration>
109+
<compilerArgs>
110+
<arg>-parameters</arg>
111+
</compilerArgs>
112+
</configuration>
113+
</plugin>
114+
<plugin>
115+
<artifactId>maven-surefire-plugin</artifactId>
116+
<version>${surefire-plugin.version}</version>
117+
<configuration>
118+
<systemPropertyVariables>
119+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
120+
<maven.home>${maven.home}</maven.home>
121+
</systemPropertyVariables>
122+
</configuration>
123+
</plugin>
124+
<plugin>
125+
<artifactId>maven-failsafe-plugin</artifactId>
126+
<version>${surefire-plugin.version}</version>
127+
<executions>
128+
<execution>
129+
<goals>
130+
<goal>integration-test</goal>
131+
<goal>verify</goal>
132+
</goals>
133+
</execution>
134+
</executions>
135+
<configuration>
136+
<systemPropertyVariables>
137+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner
138+
</native.image.path>
139+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
140+
<maven.home>${maven.home}</maven.home>
141+
</systemPropertyVariables>
142+
</configuration>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
147+
<profiles>
148+
<profile>
149+
<id>native</id>
150+
<activation>
151+
<property>
152+
<name>native</name>
153+
</property>
154+
</activation>
155+
<properties>
156+
<skipITs>false</skipITs>
157+
<quarkus.native.enabled>true</quarkus.native.enabled>
158+
</properties>
159+
</profile>
160+
</profiles>
161+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung;
2+
3+
public class Post {
4+
5+
public Long id;
6+
7+
public String title;
8+
9+
public String description;
10+
11+
Post(Long id, String title, String description) {
12+
this.id = id;
13+
this.title = title;
14+
this.description = description;
15+
}
16+
17+
Post() {
18+
}
19+
20+
Long getId() {
21+
return id;
22+
}
23+
24+
void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
String getTitle() {
29+
return title;
30+
}
31+
32+
void setTitle(String title) {
33+
this.title = title;
34+
}
35+
36+
String getDescription() {
37+
return description;
38+
}
39+
40+
void setDescription(String description) {
41+
this.description = description;
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.javahttpclient;
2+
3+
import java.util.List;
4+
5+
import jakarta.inject.Inject;
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
import jakarta.ws.rs.Produces;
9+
import jakarta.ws.rs.core.MediaType;
10+
11+
import com.baeldung.Post;
12+
13+
@Path("/java-http-client/consume-posts")
14+
public class JavaHttpClientPostResource {
15+
16+
@Inject
17+
JavaHttpClientPostService postService;
18+
19+
@GET
20+
@Produces(MediaType.APPLICATION_JSON)
21+
public List<Post> getPosts() {
22+
return postService.getPosts();
23+
}
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.javahttpclient;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import jakarta.enterprise.context.ApplicationScoped;
12+
13+
import com.baeldung.Post;
14+
import com.fasterxml.jackson.core.type.TypeReference;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
17+
@ApplicationScoped
18+
public class JavaHttpClientPostService {
19+
20+
private final HttpClient httpClient;
21+
private final ObjectMapper objectMapper;
22+
23+
public JavaHttpClientPostService() {
24+
this.httpClient = HttpClient.newHttpClient();
25+
this.objectMapper = new ObjectMapper();
26+
}
27+
28+
public List<Post> getPosts() {
29+
HttpRequest request = HttpRequest.newBuilder()
30+
.uri(URI.create("http://localhost:8080/posts"))
31+
.GET()
32+
.build();
33+
34+
try {
35+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
36+
return objectMapper.readValue(response.body(), new TypeReference<ArrayList<Post>>() {
37+
});
38+
} catch (IOException | InterruptedException e) {
39+
throw new RuntimeException("Failed to fetch posts", e);
40+
}
41+
}
42+
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//package com.baeldung.jaxrs;
2+
//
3+
//import java.util.List;
4+
//
5+
//import jakarta.inject.Inject;
6+
//import jakarta.ws.rs.GET;
7+
//import jakarta.ws.rs.Path;
8+
//import jakarta.ws.rs.Produces;
9+
//import jakarta.ws.rs.core.MediaType;
10+
//
11+
//import com.baeldung.Post;
12+
//
13+
//@Path("jax-rs/consume-posts")
14+
//public class JaxRsPostResource {
15+
//
16+
// @Inject
17+
// JaxRsPostService jaxRsPostService;
18+
//
19+
// @GET
20+
// @Produces(MediaType.APPLICATION_JSON)
21+
// public List<Post> getAllPosts() {
22+
// return jaxRsPostService.getPosts();
23+
// }
24+
//}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//package com.baeldung.jaxrs;
2+
//
3+
//import java.util.List;
4+
//
5+
//import com.baeldung.Post;
6+
//
7+
//import jakarta.enterprise.context.ApplicationScoped;
8+
//import jakarta.ws.rs.client.Client;
9+
//import jakarta.ws.rs.client.ClientBuilder;
10+
//import jakarta.ws.rs.client.WebTarget;
11+
//import jakarta.ws.rs.core.GenericType;
12+
//
13+
//@ApplicationScoped
14+
//public class JaxRsPostService {
15+
//
16+
// private final Client client;
17+
// private final WebTarget target;
18+
//
19+
// public JaxRsPostService() {
20+
// this.client = ClientBuilder.newClient();
21+
// this.target = client.target("http://localhost:8080/posts");
22+
// }
23+
//
24+
// public List<Post> getPosts() {
25+
// return target.request()
26+
// .get(new GenericType<List<Post>>() {
27+
// });
28+
// }
29+
//}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.restclient;
2+
3+
import java.util.List;
4+
5+
import jakarta.inject.Inject;
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
import jakarta.ws.rs.Produces;
9+
import jakarta.ws.rs.core.MediaType;
10+
11+
import org.eclipse.microprofile.rest.client.inject.RestClient;
12+
13+
import com.baeldung.Post;
14+
15+
@Path("/rest-client/consume-posts")
16+
public class PostClientResource {
17+
18+
@Inject
19+
@RestClient
20+
PostRestClient postRestClient;
21+
22+
@GET
23+
@Produces(MediaType.APPLICATION_JSON)
24+
public List<Post> getPosts() {
25+
return postRestClient.getAllPosts();
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.restclient;
2+
3+
import java.util.List;
4+
5+
import jakarta.ws.rs.GET;
6+
import jakarta.ws.rs.Path;
7+
import jakarta.ws.rs.Produces;
8+
import jakarta.ws.rs.core.MediaType;
9+
10+
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
11+
12+
import com.baeldung.Post;
13+
14+
@Path("/posts")
15+
@RegisterRestClient(configKey = "post-api")
16+
public interface PostRestClient {
17+
18+
@GET
19+
@Produces(MediaType.APPLICATION_JSON)
20+
List<Post> getAllPosts();
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
quarkus.http.port=9000
2+
quarkus.rest-client.post-api.url=http://localhost:8080

0 commit comments

Comments
 (0)