Return HTTP Created Status on Play! Framework

I have an action createon Play! which should return an HTTP status code Createdand redirect the client to the location of the created object.

public class SomeController extends Controller {

    public static void create() {
        Something something = new Something();
        something.save();
        response.status = StatusCode.CREATED;  // Doesn't work!
        show(something.id);
    }

    public static void show(long id) {
        render(Something.findById(id));
    }
}

See also chaining method in Play! framework documentation .

The above code returns a status code 302 Foundinstead 201 Created. What can I do to allow Play to return the correct status (and title Location)?

+5
source share
3 answers

, , , -, Show -, Show.

, ( RESTful), , create() show().

, .

  • , , ( ).
  • show() create()...

2, :

public static void create() {
    Something something = new Something();
    something.save();
    response.status = StatusCode.CREATED;
    renderTemplate("Application/show.html", something);
}
+6

:   Response.current(). Status = Http.StatusCode.CREATED;

+1

In the playback structure, a call to another action redirects, except that the called action is not public. So here is one solution:

public class SomeController extends Controller {

    public static void create() {
        Something something = new Something();
        something.save();
        response.status = StatusCode.CREATED;  // Doesn't work!
        show(something.id);
    }

    private static void show(long id) {
        render(Something.findById(id));
    }
}
0
source

All Articles