diff --git a/docs/src/main/asciidoc/resteasy-reactive.adoc b/docs/src/main/asciidoc/resteasy-reactive.adoc index bbb3bef75532f..d27aa03e9356b 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 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 to get RESTEasy Reactive to perform the desired redirect. + === Async/reactive support [[reactive]]