This project intent to be a lightweight server to provide a fake REST interface for you application.
Drop a file into a data/ directory in your classpath and it will be automatically served.
This is best deployed into a WAR file.
Any file present will be made available as is. The content type is deduced from the extension.
data/your/file.json will be available at http://localhost:port/data/your/file.json as application/json
You can request, via the header value "Content-Type" a specific result format. This will order Jexter to look for a specific file extension in your classpath.
For example:
data/your/file.json can be available at http://localhost:port/data/your/file if you add in your header Content-Type = "application/json"
Ok. This is very good but I don't need Spray just for static files!
When faking API results, you quickly need to handle parameters. More generally, it might be handy to customise the data, generate values and so on.
Jexter allows you to use Scalate templates.
data/your/dyno.json.mustache will be available at http://localhost:port/data/your/dyno.json
Any parameters you have in your request is passed to the template as value.
Given a template:
{
"title": "{{ceci}}"
}
http://localhost:port/data/your/dyno.json?ceci=cela will render:
{
"title": "cela"
}
https://github.com/athieriot/jexter/blob/master/TODO
Add the dependency:
SBT:
libraryDependencies += "com.github.athieriot" %% "jexter" % "0.5.1"
Maven:
<dependency>
<groupId>com.github.athieriot</groupId>
<artifactId>jexter_2.10</artifactId>
<version>0.5.1</version>
</dependency>
Add a new web.xml file in webapp/WEB-INF with this content:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_4.dtd">
<web-app>
<listener>
<listener-class>spray.servlet.Initializer</listener-class>
</listener>
<servlet>
<servlet-name>SprayConnectorServlet</servlet-name>
<servlet-class>spray.servlet.Servlet30ConnectorServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SprayConnectorServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
For SBT, you will want to configure the xsbt-web-plugin
This project contains two minimal projects examples to start with:
Maven: https://github.com/athieriot/jexter/tree/master/examples/maven
SBT: https://github.com/athieriot/jexter/tree/master/examples/sbt
You can optionally override values in an application.conf present in the classpath
If you deploy the project as a WAR at a different context than root. You need to specify it to Spray. In this example, Spray can respond to http://localhost:8080/maven
spray.servlet {
root-path = "/maven"
}
The root path of Jexter is where your files can be found in the class path.
jexter {
rootPath = "data"
}
With the default JVM configuration, you might encounter an out of memory exception using Jexter.
Increase the value of the property MaxPermSize
should resolve the problem.
The JAVA_OPTS option to do so is:
-XX:MaxPermSize=256M
Inspired from Dyson