How to redirect user and send message to landing page using Jersey and Dropwizard?

I have a POJO resource that defines some HTTP endpoints and returns Dropwizard Views. Some of these endpoints just take an action (like updating the db) and then redirect the user to a different endpoint. For example, a user is in a location GET /foo

and submits a form. This directs them to the endpoint POST /foo/submit

, processes them, and then forwards them to GET /foo/done

. This prevents the form from being resubmitted if they refresh the page, for example. This forwarding is currently done using the Jersey method Response.seeOther () (returning a response instead of a view).

What I would like to do is when the processing method processes their submission, generates some kind of message (error, warning, success, etc.) and passes that message to the page we are forwarding. For example, in GET /foo/done

, I would like him to say at the top of the page, "Submission complete!" or "Feeding failed because ...".

I have done a few searches and many people suggest throwing a WebApplicationException except that not all of my cases are errors. Sometimes I would just like to confirm the confirmation of a successful action. But I can't figure out how to get the receive method to receive the message. I've done this before in Python, having a way of processing that accepts an optional dictionary, but unfortunately I'm on Java 7, so I don't have the ability to provide methods with optional parameters with default values.

Thank you for your help.

+3


source to share


1 answer


Redirects will just send GET requests. The GET request should not have any authority. To send arbitrary data using GET requests, just send it to the query string (of course, there shouldn't be any sensitive information here). for example



@Path("foo")
public class FooResource {

    @GET
    @Path("done")
    public String getDone(@QueryParam("message") String message) {
        return message;
    }

    @POST
    @Path("submit")
    public Response postData() {
        String message = UriComponent.encode(
                "You have perefected submitting!", 
                UriComponent.Type.QUERY_PARAM_SPACE_ENCODED);
        return Response.seeOther(URI.create("/api/foo/done?message=" + message)).build();
    }
}

      

+2


source







All Articles