Andreas Fritsch
Andreas Fritsch

Reputation: 242

play framework content type split

I want to distinguish between text/html and application/json, but my code returns always 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");
}

Upvotes: 2

Views: 121

Answers (1)

Eugen Hotaj
Eugen Hotaj

Reputation: 383

If the request accepts both html as well as json (this is the case in a lot of scenarios) then your first if statement for the html will always execute and you will return a response before getting to the if statment for thejson part.

Upvotes: 1

Related Questions