Bra*_*ace 13 rest httpresponse playframework
我刚刚开始使用REST,我一直在阅读这篇文章和上面提到的关于REST 响应代码的书.但是,当我查看Play的Controller类时,它似乎仅限于返回
这似乎遗漏了一些可能有用的代码:
毕竟不需要那些吗?Play会自动处理这些情况吗?
此外,似乎一个控制器无法很好地处理相同资源的REST请求和正常网页请求,因为始终返回网页200.我错过了什么吗?
Cod*_*nci 12
查看对象上的播放源代码(播放1.1)play.mvc.Http.StatusCode,播放似乎具有以下代码
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;
Run Code Online (Sandbox Code Playgroud)
这将表示确认您已识别的某些代码,例如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
}
Run Code Online (Sandbox Code Playgroud)
在当前的Play版本中,您必须使用status().例:
status(201, jsonData);
Run Code Online (Sandbox Code Playgroud)
在Scala中,它应该像在官方文档中获取的示例一样工作:
Status(488)("Strange response type")
Run Code Online (Sandbox Code Playgroud)