java play框架内容类型拆分
我想区分text/html和application/json,但我的代码总是返回text/html
public static Result GetAll() {
List<ErrorType> dataList = ErrorType.find.where().orderBy("id asc").findList();
title = Thread.currentThread().getStackTrace()[1].getMethodName() + "/" + title;
//html
if (request().accepts("text/html")) {
return ok(index.render(dataList, title));
}
//json
if (request().accepts("application/json")) {
return ok(Json.toJson(dataList));
}
//other
return badRequest("accepts only text/html or application/json");
}
# 1 楼答案
如果请求同时接受
html
和json
(在很多情况下都是这样),那么html
的第一个if
语句将始终执行,并且在到达json
部分的If语句之前,将返回一个响应