有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    如果请求同时接受htmljson(在很多情况下都是这样),那么html的第一个if语句将始终执行,并且在到达json部分的If语句之前,将返回一个响应