Skip to content

Commit 8d1d2d5

Browse files
authored
Merge pull request quarkusio#46879 from geoand/quarkusio#46849
Use RestResponse in the implementation of REST Data Panache endpoints
2 parents 1bae4fa + b5e90b6 commit 8d1d2d5

File tree

19 files changed

+111
-148
lines changed

19 files changed

+111
-148
lines changed

docs/src/main/asciidoc/rest-data-panache.adoc

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include::_attributes.adoc[]
1313
A lot of web applications are monotonous CRUD applications with REST APIs that are tedious to write.
1414
To streamline this task, REST Data with Panache extension can generate the basic CRUD endpoints for your entities and repositories.
1515

16-
Currently, this extension supports Hibernate ORM and MongoDB with Panache and can generate CRUD resources that work with `application/json` and `application/hal+json` content.
16+
Currently, this extension supports Hibernate ORM and MongoDB with Panache and can generate CRUD resources that work with `application/json` and `application/hal+json` content and generates REST Resources backed by Quarkus REST.
1717

1818
== Setting up REST Data with Panache
1919

@@ -22,22 +22,20 @@ Please, check out the next compatibility table to use the right one according to
2222

2323
.Compatibility Table
2424
|===
25-
|Extension |Status |Hibernate |RESTEasy
25+
|Extension |Status |Hibernate
2626

2727
|<<hr-hibernate-orm,quarkus-hibernate-orm-rest-data-panache>>
2828
|`Stable`
2929
|`ORM`
30-
|`Classic and Reactive`
3130

3231
|<<hr-hibernate-reactive,quarkus-hibernate-reactive-rest-data-panache>>
3332
|`Experimental`
3433
|`Reactive`
35-
|`Reactive`
3634

3735
|<<hr-mongodb,quarkus-mongodb-rest-data-panache>>
3836
|`Experimental`
3937
|`ORM`
40-
|`Classic and Reactive`
38+
4139
|===
4240

4341
[[hr-hibernate-orm]]
@@ -46,7 +44,7 @@ Please, check out the next compatibility table to use the right one according to
4644
* Add the required dependencies to your build file
4745
** Hibernate ORM REST Data with Panache extension (`quarkus-hibernate-orm-rest-data-panache`)
4846
** A JDBC driver extension (`quarkus-jdbc-postgresql`, `quarkus-jdbc-h2`, `quarkus-jdbc-mariadb`, ...)
49-
** One of the RESTEasy JSON serialization extensions (the extension supports both Quarkus REST (formerly RESTEasy Reactive) and RESTEasy Classic)
47+
** One of the REST JSON serialization extensions (such as `quarkus-rest-jackson`)
5048

5149
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"]
5250
.pom.xml
@@ -60,18 +58,10 @@ Please, check out the next compatibility table to use the right one according to
6058
<artifactId>quarkus-jdbc-postgresql</artifactId>
6159
</dependency>
6260
63-
<!-- Use this if you are using Quarkus REST -->
6461
<dependency>
6562
<groupId>io.quarkus</groupId>
6663
<artifactId>quarkus-rest-jackson</artifactId>
6764
</dependency>
68-
69-
<!-- Use this if you are going to use RESTEasy Classic -->
70-
<!--
71-
<dependency>
72-
<groupId>io.quarkus</groupId>
73-
<artifactId>quarkus-resteasy-jackson</artifactId>
74-
</dependency>
7565
-->
7666
----
7767

@@ -81,11 +71,7 @@ Please, check out the next compatibility table to use the right one according to
8171
implementation("io.quarkus:quarkus-hibernate-orm-rest-data-panache")
8272
implementation("io.quarkus:quarkus-jdbc-postgresql")
8373
84-
// Use this if you are using Quarkus REST
8574
implementation("io.quarkus:quarkus-rest-jackson")
86-
87-
// Use this if you are going to use RESTEasy Classic
88-
// implementation("io.quarkus:quarkus-resteasy-jackson")
8975
----
9076

9177
* Implement the Panache entities and/or repositories as explained in the xref:hibernate-orm-panache.adoc[Hibernate ORM with Panache] guide.
@@ -143,14 +129,6 @@ To see the Hibernate ORM REST Data with Panache in action, you can check out the
143129
<groupId>io.quarkus</groupId>
144130
<artifactId>quarkus-rest-jackson</artifactId>
145131
</dependency>
146-
147-
<!-- Use this if you are going to use RESTEasy Classic -->
148-
<!--
149-
<dependency>
150-
<groupId>io.quarkus</groupId>
151-
<artifactId>quarkus-resteasy-jackson</artifactId>
152-
</dependency>
153-
-->
154132
----
155133

156134
[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"]
@@ -160,9 +138,6 @@ implementation("io.quarkus:quarkus-mongodb-rest-data-panache")
160138
161139
// Use this if you are using Quarkus REST
162140
implementation("io.quarkus:quarkus-rest-jackson")
163-
164-
// Use this if you are going to use RESTEasy Classic
165-
// implementation("io.quarkus:quarkus-resteasy-jackson")
166141
----
167142

168143
* Implement the Panache entities and/or repositories as explained in the xref:mongodb-panache.adoc[MongoDB with Panache] guide.
@@ -233,17 +208,18 @@ public class PeopleResourceJaxRs { // The actual class name is going to be uniqu
233208
@GET
234209
@Path("{id}")
235210
@Produces("application/json")
236-
public Person get(@PathParam("id") Long id){
211+
public RestResponse<Person> get(@PathParam("id") Long id){
237212
Person person = resource.get(id);
238213
if (person == null) {
239-
throw new WebApplicationException(404);
214+
return ResponseBuilder.create(404).build();
215+
} else {
216+
return ResponseBuilder.ok(var3).build();
240217
}
241-
return person;
242218
}
243219
244220
@GET
245221
@Produces("application/json")
246-
public Response list(@QueryParam("sort") List<String> sortQuery,
222+
public RestResponse<Person> list(@QueryParam("sort") List<String> sortQuery,
247223
@QueryParam("page") @DefaultValue("0") int pageIndex,
248224
@QueryParam("size") @DefaultValue("20") int pageSize) {
249225
Page page = Page.of(pageIndex, pageSize);
@@ -254,16 +230,16 @@ public class PeopleResourceJaxRs { // The actual class name is going to be uniqu
254230
255231
@GET
256232
@Path("/count")
257-
public long count() {
258-
return resource.count();
233+
public RestResponse<Long> count() {
234+
return ResponseBuilder.ok(var1.count()).build()
259235
}
260236
261237
@Transactional
262238
@POST
263239
@Consumes("application/json")
264240
@Produces("application/json")
265-
public Response add(Person personToSave) {
266-
Person person = resource.add(person);
241+
public RestResponse<Person> add(Person personToSave) {
242+
Person person = resource.add(personToSave);
267243
// ... build a new location URL and return 201 response with an entity
268244
}
269245
@@ -272,10 +248,10 @@ public class PeopleResourceJaxRs { // The actual class name is going to be uniqu
272248
@Path("{id}")
273249
@Consumes("application/json")
274250
@Produces("application/json")
275-
public Response update(@PathParam("id") Long id, Person personToSave) {
251+
public RestResponse<Person> update(@PathParam("id") Long id, Person personToSave) {
276252
if (resource.get(id) == null) {
277253
Person person = resource.update(id, personToSave);
278-
return Response.status(204).build();
254+
return ResponseBuilder.create(204).build();
279255
}
280256
Person person = resource.update(id, personToSave);
281257
// ... build a new location URL and return 201 response with an entity
@@ -284,10 +260,8 @@ public class PeopleResourceJaxRs { // The actual class name is going to be uniqu
284260
@Transactional
285261
@DELETE
286262
@Path("{id}")
287-
public void delete(@PathParam("id") Long id) {
288-
if (!resource.delete(id)) {
289-
throw new WebApplicationException(404);
290-
}
263+
public RestResponse<Person> delete(@PathParam("id") Long id) {
264+
return !var2.delete(id) ? ResponseBuilder.create(404).build() : ResponseBuilder.create(204).build();
291265
}
292266
}
293267
----

extensions/panache/hibernate-orm-rest-data-panache/deployment/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
</dependency>
3838
<dependency>
3939
<groupId>io.quarkus</groupId>
40-
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
40+
<artifactId>quarkus-rest-jackson-deployment</artifactId>
4141
<scope>test</scope>
4242
</dependency>
4343
<dependency>
4444
<groupId>io.quarkus</groupId>
45-
<artifactId>quarkus-resteasy-links-deployment</artifactId>
45+
<artifactId>quarkus-rest-links-deployment</artifactId>
4646
<scope>test</scope>
4747
</dependency>
4848
<dependency>

extensions/panache/rest-data-panache/deployment/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
<artifactId>quarkus-hal-deployment</artifactId>
3535
</dependency>
3636

37-
<dependency>
38-
<groupId>io.quarkus</groupId>
39-
<artifactId>quarkus-resteasy-links-deployment</artifactId>
40-
<optional>true</optional>
41-
</dependency>
4237
<dependency>
4338
<groupId>io.quarkus</groupId>
4439
<artifactId>quarkus-rest-links-deployment</artifactId>

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/AddMethodImplementor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import jakarta.ws.rs.core.Response;
1010
import jakarta.ws.rs.core.UriInfo;
1111

12+
import org.jboss.resteasy.reactive.RestResponse;
13+
1214
import io.quarkus.deployment.Capabilities;
1315
import io.quarkus.gizmo.ClassCreator;
1416
import io.quarkus.gizmo.FieldDescriptor;
@@ -104,7 +106,8 @@ public AddMethodImplementor(Capabilities capabilities) {
104106
protected void implementInternal(ClassCreator classCreator, ResourceMetadata resourceMetadata,
105107
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
106108
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
107-
isNotReactivePanache() ? responseType() : uniType(resourceMetadata.getEntityType()),
109+
isNotReactivePanache() ? responseType(resourceMetadata.getEntityType())
110+
: uniType(resourceMetadata.getEntityType()),
108111
param("entity", resourceMetadata.getEntityType()), param("uriInfo", UriInfo.class));
109112

110113
// Add method annotations
@@ -115,7 +118,7 @@ protected void implementInternal(ClassCreator classCreator, ResourceMetadata res
115118
addConsumesAnnotation(methodCreator, APPLICATION_JSON);
116119
addProducesJsonAnnotation(methodCreator, resourceProperties);
117120
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
118-
addOpenApiResponseAnnotation(methodCreator, Response.Status.CREATED, resourceMetadata.getEntityType());
121+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.CREATED, resourceMetadata.getEntityType());
119122
addSecurityAnnotations(methodCreator, resourceProperties);
120123
// Add parameter annotations
121124
if (hasValidatorCapability()) {

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/CountMethodImplementor.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator.responseType;
55
import static io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator.uniType;
66

7-
import jakarta.ws.rs.core.Response;
7+
import org.jboss.resteasy.reactive.RestResponse;
88

99
import io.quarkus.deployment.Capabilities;
1010
import io.quarkus.gizmo.ClassCreator;
@@ -75,20 +75,16 @@ protected void implementInternal(ClassCreator classCreator, ResourceMetadata res
7575
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
7676
// Method parameters: sort strings, page index, page size, uri info
7777
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(RESOURCE_METHOD_NAME, classCreator,
78-
isNotReactivePanache() ? responseType() : uniType(Long.class));
78+
isNotReactivePanache() ? responseType(Long.class) : uniType(Long.class));
7979

8080
// Add method annotations
8181
addGetAnnotation(methodCreator);
8282
addProducesAnnotation(methodCreator, APPLICATION_JSON);
8383
addPathAnnotation(methodCreator, appendToPath(resourceProperties.getPath(RESOURCE_METHOD_NAME), RESOURCE_METHOD_NAME));
8484
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
85-
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, Long.class, false);
85+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.OK, Long.class, false);
8686
addSecurityAnnotations(methodCreator, resourceProperties);
87-
if (!isResteasyClassic()) {
88-
// We only add the Links annotation in Resteasy Reactive because Resteasy Classic ignores the REL parameter:
89-
// it always uses "list" for GET methods, so it interferes with the list implementation.
90-
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
91-
}
87+
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
9288

9389
ResultHandle resource = methodCreator.readInstanceField(resourceField, methodCreator.getThis());
9490

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/DeleteMethodImplementor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import jakarta.ws.rs.core.Response;
99

10+
import org.jboss.resteasy.reactive.RestResponse;
11+
1012
import io.quarkus.deployment.Capabilities;
1113
import io.quarkus.gizmo.BranchResult;
1214
import io.quarkus.gizmo.ClassCreator;
@@ -84,7 +86,8 @@ public DeleteMethodImplementor(Capabilities capabilities) {
8486
protected void implementInternal(ClassCreator classCreator, ResourceMetadata resourceMetadata,
8587
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
8688
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
87-
isNotReactivePanache() ? responseType() : uniType(resourceMetadata.getEntityType()),
89+
isNotReactivePanache() ? responseType(resourceMetadata.getEntityType())
90+
: uniType(resourceMetadata.getEntityType()),
8891
param("id", resourceMetadata.getIdType()));
8992

9093
// Add method annotations
@@ -93,7 +96,7 @@ protected void implementInternal(ClassCreator classCreator, ResourceMetadata res
9396
addPathParamAnnotation(methodCreator.getParameterAnnotations(0), "id");
9497
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
9598
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
96-
addOpenApiResponseAnnotation(methodCreator, Response.Status.NO_CONTENT);
99+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.NO_CONTENT);
97100
addSecurityAnnotations(methodCreator, resourceProperties);
98101

99102
ResultHandle resource = methodCreator.readInstanceField(resourceField, methodCreator.getThis());

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/GetMethodImplementor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import jakarta.ws.rs.core.Response;
99

10+
import org.jboss.resteasy.reactive.RestResponse;
11+
1012
import io.quarkus.deployment.Capabilities;
1113
import io.quarkus.gizmo.BranchResult;
1214
import io.quarkus.gizmo.ClassCreator;
@@ -86,15 +88,16 @@ public GetMethodImplementor(Capabilities capabilities) {
8688
protected void implementInternal(ClassCreator classCreator, ResourceMetadata resourceMetadata,
8789
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
8890
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
89-
isNotReactivePanache() ? responseType() : uniType(resourceMetadata.getEntityType()),
91+
isNotReactivePanache() ? responseType(resourceMetadata.getEntityType())
92+
: uniType(resourceMetadata.getEntityType()),
9093
param("id", resourceMetadata.getIdType()));
9194

9295
// Add method annotations
9396
addPathAnnotation(methodCreator, appendToPath(resourceProperties.getPath(RESOURCE_METHOD_NAME), "{id}"));
9497
addGetAnnotation(methodCreator);
9598
addProducesJsonAnnotation(methodCreator, resourceProperties);
9699
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
97-
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, resourceMetadata.getEntityType());
100+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.OK, resourceMetadata.getEntityType());
98101
addSecurityAnnotations(methodCreator, resourceProperties);
99102

100103
addPathParamAnnotation(methodCreator.getParameterAnnotations(0), "id");

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/ListMethodImplementor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import jakarta.ws.rs.core.UriInfo;
3232

3333
import org.jboss.jandex.Type;
34+
import org.jboss.resteasy.reactive.RestResponse;
3435

3536
import io.quarkus.deployment.Capabilities;
3637
import io.quarkus.gizmo.AnnotatedElement;
@@ -190,7 +191,8 @@ private void implementPaged(ClassCreator classCreator, ResourceMetadata resource
190191
param.getClazz()));
191192
}
192193
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(getMethodName(), classCreator,
193-
isNotReactivePanache() ? responseType() : uniType(resourceMetadata.getEntityType()),
194+
isNotReactivePanache() ? responseType(resourceMetadata.getEntityType())
195+
: uniType(resourceMetadata.getEntityType()),
194196
parameters.toArray(new SignatureMethodCreator.Parameter[0]));
195197

196198
// Add method annotations
@@ -199,7 +201,7 @@ private void implementPaged(ClassCreator classCreator, ResourceMetadata resource
199201
addProducesJsonAnnotation(methodCreator, resourceProperties);
200202
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
201203
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
202-
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, resourceMetadata.getEntityType(), true);
204+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.OK, resourceMetadata.getEntityType(), true);
203205
addSecurityAnnotations(methodCreator, resourceProperties);
204206
addSortQueryParamValidatorAnnotation(methodCreator);
205207
addQueryParamAnnotation(methodCreator.getParameterAnnotations(0), "sort");
@@ -276,7 +278,8 @@ private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resou
276278
param.getClazz()));
277279
}
278280
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(getMethodName(), classCreator,
279-
isNotReactivePanache() ? responseType() : uniType(resourceMetadata.getEntityType()),
281+
isNotReactivePanache() ? responseType(resourceMetadata.getEntityType())
282+
: uniType(resourceMetadata.getEntityType()),
280283
parameters.toArray(new SignatureMethodCreator.Parameter[0]));
281284

282285
// Add method annotations
@@ -285,7 +288,7 @@ private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resou
285288
addProducesJsonAnnotation(methodCreator, resourceProperties);
286289
addLinksAnnotation(methodCreator, resourceProperties, resourceMetadata.getEntityType(), REL);
287290
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
288-
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, resourceMetadata.getEntityType(), true);
291+
addOpenApiResponseAnnotation(methodCreator, RestResponse.Status.OK, resourceMetadata.getEntityType(), true);
289292
addSecurityAnnotations(methodCreator, resourceProperties);
290293
addQueryParamAnnotation(methodCreator.getParameterAnnotations(0), "sort");
291294
addQueryParamAnnotation(methodCreator.getParameterAnnotations(1), "namedQuery");

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/OverrideUserMethodImplementor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ protected void addLinksAnnotation(AnnotatedElement element, ResourceProperties r
9191
return;
9292
}
9393

94-
String linksAnnotationName = isResteasyClassic() ? "org.jboss.resteasy.links.LinkResource"
95-
: "io.quarkus.resteasy.reactive.links.RestLink";
94+
String linksAnnotationName = "io.quarkus.resteasy.reactive.links.RestLink";
9695
// Add the links annotation if and only if the user didn't add it.
9796
if (!methodInfo.hasAnnotation(linksAnnotationName)) {
9897
super.addLinksAnnotation(element, resourceProperties, entityClassName, rel);

0 commit comments

Comments
 (0)