简体   繁体   中英

Playframework 2.2 Java notModified

I'm using playframework 2.2.0 with Java. How can I return Not Modified from my controller actions?

There are several methods in the Controller superClass: ok(), noContent() etc, but not notModified().

Looking at the source code for play I can see:

val NotModified = SimpleResult(header = ResponseHeader(NOT_MODIFIED), body = Enumerator.empty,
connection = HttpConnection.KeepAlive)

in play.api.mvc.Results. But how do I wrap a SimpleResult in something which can be returned by Java controller?

the method wants to return a Result:

public interface Result {
    scala.concurrent.Future<play.api.mvc.SimpleResult> getWrappedResult();
}

but I don't know how to generate a Future from Java. (I tried with scala.concurrent.Future$.MODULE$... but it's not visible to my java code)

Instead of something like ok(), try this:

return status(304, "Content not modified, dude.");

Reference: http://www.playframework.com/documentation/2.2.0/JavaActions

It looks like play.api.mvc.Results, in the Scala API, actually has a NotModified generator, but the Java API does not. You can't use anything from the Scala API if you're going with the Java API. Seems like the Java API is the Play Framework's unloved child.

In summary, returning status 304 is much simpler than trying to drag in components from the Play Scala API and use them from Java. HTTP response code 304 = Content Not Modified.

See the list of codes here: http://www.w3.org/Protocols/HTTP/HTRESP.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM