diff --git a/docs/src/main/asciidoc/resteasy-reactive.adoc b/docs/src/main/asciidoc/resteasy-reactive.adoc index bbb3bef75532f..93a0b08e73cbb 100644 --- a/docs/src/main/asciidoc/resteasy-reactive.adoc +++ b/docs/src/main/asciidoc/resteasy-reactive.adoc @@ -780,6 +780,79 @@ public class Endpoint { } ---- +=== Redirect support + +When handling a `@POST`, `@PUT` or `@DELETE` endpoint, it is common practice to redirect to a @GET endpoint after the action has been performed, in order to allow the user to reload the page without triggering the action a second time. +There are multiple ways to achieve this. + +==== Using RestResponse + +Using `RestResponse` as the return type while making sure the proper redirection URI is created can be done as in the following example: + +[source,java] +---- +package org.acme.rest; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.UriInfo; +import org.jboss.resteasy.reactive.RestResponse; + +@Path("/fruits") +public class FruitResource { + + public static class Fruit { + public Long id; + public String name; + public String description; + + public Fruit() { + } + + public Fruit(Long id, String name, String description) { + this.id = id; + this.name = name; + this.description = description; + } + } + + private final Map fruits = new ConcurrentHashMap<>(); + private final AtomicLong ids = new AtomicLong(0); + + + public FruitResource() { + Fruit apple = new Fruit(ids.incrementAndGet(), "Apple", "Winter fruit"); + fruits.put(apple.id, apple); + Fruit pinneapple = new Fruit(ids.incrementAndGet(), "Pineapple", "Tropical fruit"); + fruits.put(pinneapple.id, pinneapple); + } + + // when invoked, this method will result in an HTTP redirect to the GET method that obtains the fruit by id + @POST + public RestResponse add(Fruit fruit, @Context UriInfo uriInfo) { + fruit.id = ids.incrementAndGet(); + fruits.put(fruit.id, fruit); + // seeOther results in an HTTP 303 response with the Location header set to the value of the URI + return RestResponse.seeOther(uriInfo.getAbsolutePathBuilder().path(Long.toString(fruit.id)).build()); + } + + @GET + @Path("{id}") + public Fruit byId(Long id) { + return fruits.get(id); + } +} +---- + +==== Using RedirectException + +Users can also throw `javax.ws.rs.RedirectionException` from a method body in order to get RESTEasy Reactive to perform the desired redirect. + === Async/reactive support [[reactive]] diff --git a/extensions/spring-web/resteasy-reactive/deployment/src/main/java/io/quarkus/spring/web/deployment/SpringWebResteasyReactiveProcessor.java b/extensions/spring-web/resteasy-reactive/deployment/src/main/java/io/quarkus/spring/web/deployment/SpringWebResteasyReactiveProcessor.java index 4a9b50fbedc87..3df1114549a51 100644 --- a/extensions/spring-web/resteasy-reactive/deployment/src/main/java/io/quarkus/spring/web/deployment/SpringWebResteasyReactiveProcessor.java +++ b/extensions/spring-web/resteasy-reactive/deployment/src/main/java/io/quarkus/spring/web/deployment/SpringWebResteasyReactiveProcessor.java @@ -3,6 +3,7 @@ import static org.jboss.jandex.AnnotationInstance.create; import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DEFAULT_VALUE; import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_COOKIE_PARAM; +import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_HEADER_PARAM; import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_MATRIX_PARAM; import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_PATH_PARAM; import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_QUERY_PARAM; @@ -280,7 +281,7 @@ public void transform(TransformationContext transformationContext) { if (annotationName.equals(REQUEST_PARAM)) { jaxRsAnnotation = REST_QUERY_PARAM; } else if (annotationName.equals(REQUEST_HEADER)) { - jaxRsAnnotation = REST_QUERY_PARAM; + jaxRsAnnotation = REST_HEADER_PARAM; } else if (annotationName.equals(COOKIE_VALUE)) { jaxRsAnnotation = REST_COOKIE_PARAM; } else {