A jump start module for creating webapp modules. build.gradle:
repositories {
maven {
name = 'sonatype-snapshots'
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
compile 'com.blacklocus:jersey-jump:1.0-SNAPSHOT'
}
To generate an all-in-one fat jar, add this to your module's build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'eu.appsatori:gradle-fatjar-plugin:0.2-rc1'
}
}
apply plugin: 'fatjar'
fatJar {
classifier = 'fat'
manifest.attributes 'Main-Class': 'com.blacklocus.webapp.RunServer'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
}
You'll need to specify the base scan package as the default (scan all) won't work across jars. I don't know why this is, but it's pretty annoying.
For fatJar this would be a -D like -Dbase.pkg=your.company
. For the gradle application plugin, do it through JAVA_OPTS
:
export JAVA_OPTS="-Dbase.pkg=com.company"
Create classes that extend com.blacklocus.webapp.base.BaseJsonResource
and annotate them in the usual Jersey ways.
Note that RunServer initializes Jersey with a SingletonFactory
, so all web resources and providers must be threadsafe.
Example:
@Path("/stuff")
public class Stuff extends BaseJsonResource {
private final MyService myService;
// Jersey has a dead simple DI mechanic. To create injectable instances, annotate them with @Provider.
public Stuff(@InjectParam MyService myService) {
this.myService = myService;
}
@GET
public String helloWorld() {
return "hello world";
}
@GET
public ResponsePojo example(@QueryParam("isContrived") Boolean isContrived) {
ResponsePojo response = new ResponsePojo();
response.success = isContrived;
// Note that by extending BaseJsonResource, objects will be serialized to/from json using
// com.blacklocus.webapp.app.DefaultObjectMapper
return response;
}
}
class ResponsePojo {
public Boolean success;
}
Place them in your project directory under src/main/webapp/static/
. To mark them as source files in both IntelliJ
and in the fat jar build, build.gradle:
sourceSets {
main.resources.srcDir 'src/main/webapp'
}
The static/ directory will end up at the root of the fat jar.
IntelliJ copies files to a temporary directory on build/run. If you would like to be able to edit static files while
your webapp is running, this property will cause RunServer to configure Jetty to point directly at your source
directories rather than the temporary build out generated by IntelliJ. Notice here, the static/
dir is not implied
and must be explicitly included:
-Dbase.static_dirs=src/main/webapp/static/
This property does accept multiple locations, if such a layering during development is desired.
-Dbase.static_dirs=submodule-1/src/main/webapp/static/;submodule-0/src/main/webapp/static/
Copyright 2013 BlackLocus
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.