在Play中返回RESTful响应代码

我刚刚开始使用REST,我一直在阅读这篇文章和上面提到的关于REST响应代码的书。但是,当我查看Play的Controller类时,它似乎仅限于返回 200 - 好的 301 - 永久移动 302 - 发现 304 - 未修改 400 - 错误请求 401 - 未授权 403 - 禁止 404 - 未找到 5XX 这似乎遗漏了一些可能有用的代码: 201 - 创建(成功JSON帖子的良好响应?) 202 - 已接受(对于排队请求) 204 - 无内容(成功PUT / POST / DELETE的可能响应) 307 - 临时重定向 405 - 不允许的方法 406 - 不可接受 409 - 冲突 410 - 走了 415 - 不支持的媒体类型(当没有定义JSON模板时,这似乎是对JSON格式请求的适当响应) 毕竟不需要那些吗? Play会自动处理这些情况吗? 此外,似乎一个控制器无法很好地处理相同资源的REST请求和正常网页请求,因为网页总是以
200
返回。我错过了什么吗?     
已邀请:
查看
play.mvc.Http.StatusCode
对象的播放源代码(播放1.1),播放似乎具有以下代码
public static final int OK = 200;
public static final int CREATED = 201;
public static final int ACCEPTED = 202;
public static final int PARTIAL_INFO = 203;
public static final int NO_RESPONSE = 204;
public static final int MOVED = 301;
public static final int FOUND = 302;
public static final int METHOD = 303;
public static final int NOT_MODIFIED = 304;
public static final int BAD_REQUEST = 400;
public static final int UNAUTHORIZED = 401;
public static final int PAYMENT_REQUIERED = 402;
public static final int FORBIDDEN = 403;
public static final int NOT_FOUND = 404;
public static final int INTERNAL_ERROR = 500;
public static final int NOT_IMPLEMENTED = 501;
public static final int OVERLOADED = 502;
public static final int GATEWAY_TIMEOUT = 503;
这将表示确认您已识别的某些代码,例如201,202,204。但是,值307,405,406,409,410和415不存在。 此外,201,202,204被确认,但未在源代码中的任何其他地方引用。因此,除非Netty服务器或其中一个提供的jar文件正在管理Play(我不确定它可以做),我无法看到Play如何在不知道代码库的情况下神奇地处理这些情况。 查看renderJSON的代码,它似乎没有将状态代码设置为发送结果的一部分(因此使用默认值200),因此以下hack可能有效。
public static void myJsonAction() {
    response.status = 201;
    renderJSON(jsonString); // replace with your JSON String
}
    
在当前的Play版本中,您必须使用
status()
。例:
status(201, jsonData);
在Scala中,它应该像在官方文档中获取的示例一样工作:
Status(488)("Strange response type")
    

要回复问题请先登录注册