WHAT I WANT
I'm working on a maven-jetty-plugin that uses jersey to map resources. How can I cache the version number for 5 hours so that I don't have to GET it every time the page loads?
MY CODE
Here is the html code that will contain the version number once it is loaded:
...
<footer>
<hr/>
VERSION: <span id="version-container">...Loading...</span>
</footer>
Here is the web.xml with the servlet mapping to 'localhost:8080/rest/':
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Here is the java code to handle a GET at 'localhost:8080/rest/version':
/* ... package & imports omitted ... */
@Path("/")
public class RootResource {
@GET
@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
public String versionAsJson(){
return String.format("{ \"version\": \"%s\"}", "v.01");
}
}
Here is the javascript code to load the version after the page loads (using jQuery):
$(document).ready(function() {
$.get("http://localhost:8080/rest/version",
function(response){
$("#version-container").html(response.version);
},
"json"
).error(function(){
$("#version-container").html("[FAILED TO GET VERSION]");
});
}