dea*_*mon 5 java rest http-status-codes playframework playframework-1.x
我create在Play中有一个动作!框架控制器,应返回HTTP状态代码Created并将客户端重定向到创建的对象的位置.
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));
}
}
Run Code Online (Sandbox Code Playgroud)
另请参阅Play中的方法链接!框架文档.
上面的代码返回状态代码302 Found而不是201 Created.我该怎么做让Play返回正确的状态(和Location标题)?
这种情况发生的原因是,一旦你创造了你的东西,你就会Show通过调用这个show动作来告诉你的东西.
为了实现这一点,play正在执行重定向(以维持其RESTful状态),告诉浏览器由于调用该create()操作,它现在必须重定向到该show()操作.
所以,你有几个选择.
要使用选项2,它可能如下所示:
public static void create() {
Something something = new Something();
something.save();
response.status = StatusCode.CREATED;
renderTemplate("Application/show.html", something);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4166 次 |
| 最近记录: |