Last active
January 5, 2018 00:08
-
-
Save hisui/7829c50e848ba0277b262925d377099e to your computer and use it in GitHub Desktop.
JAX-WS on Jetty + Google Guice in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
ext.kotlin_version = '1.2.0' | |
repositories { mavenCentral() } | |
dependencies { | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
apply plugin: 'kotlin' | |
def jersey_version = "2.23.1" | |
def jetty_version = "9.4.6.v20170531" | |
repositories { mavenCentral() } | |
dependencies { | |
compile fileTree(dir: 'lib', include: ['*.jar']) | |
// JAX-WS | |
compile 'javax.ws.rs:javax.ws.rs-api:2.0.0' | |
// Kotlin | |
compile group: "org.jetbrains.kotlin", version: kotlin_version, name: "kotlin-stdlib" | |
compile group: "org.jetbrains.kotlin", version: kotlin_version, name: "kotlin-reflect" | |
testCompile group: "org.jetbrains.kotlin", version: kotlin_version, name: "kotlin-test" | |
testCompile group: "org.jetbrains.kotlin", version: kotlin_version, name: "kotlin-test-junit" | |
// Jersey | |
compile group: 'org.glassfish.jersey.containers', version: jersey_version, name: 'jersey-container-servlet' | |
compile group: 'org.glassfish.jersey.containers', version: jersey_version, name: 'jersey-container-servlet-core' | |
compile group: 'org.glassfish.jersey.media', version: jersey_version, name: 'jersey-media-multipart' | |
compile group: 'org.glassfish.jersey.media', version: jersey_version, name: 'jersey-media-json-jackson' | |
// Jetty | |
compile group: 'org.eclipse.jetty', version: jetty_version, name: 'jetty-server' | |
compile group: 'org.eclipse.jetty', version: jetty_version, name: 'jetty-servlet' | |
// Google Guice (DI Container) | |
compile group: 'com.google.inject', name: 'guice', version: '4.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.segfault.playground.aswj | |
import com.google.inject.Inject | |
import com.google.inject.Singleton | |
import javax.ws.rs.GET | |
import javax.ws.rs.Path | |
import javax.ws.rs.Produces | |
import javax.ws.rs.core.MediaType | |
import javax.ws.rs.core.Response | |
@Singleton | |
@Path("/api") | |
class EntryPoint { | |
@Inject constructor() | |
@Inject | |
lateinit var myService: MyService | |
@GET | |
@Path("debug") | |
@Produces(MediaType.APPLICATION_JSON) | |
fun test(): Response { | |
return Response.ok(Debug("foo", System.currentTimeMillis())).build() | |
} | |
} | |
data class Debug(val name: String, val time: Long) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.segfault.playground.aswj | |
import com.google.inject.AbstractModule | |
import com.google.inject.Guice | |
import org.eclipse.jetty.server.Server | |
import org.eclipse.jetty.servlet.ServletContextHandler | |
import org.eclipse.jetty.servlet.ServletHolder | |
import org.glassfish.jersey.server.ResourceConfig | |
import org.glassfish.jersey.servlet.ServletContainer | |
object Main { | |
@JvmStatic fun main(args: Array<String>) { | |
val injector = Guice.createInjector(object : AbstractModule() { | |
override fun configure() { | |
} | |
}) | |
val server = Server(8080) | |
server.handler = ServletContextHandler(ServletContextHandler.SESSIONS).apply { | |
contextPath = "/" | |
addServlet(ServletHolder(ServletContainer(object: ResourceConfig() { | |
init { | |
register(injector.getInstance(EntryPoint::class.java)) | |
} | |
})), "/*") | |
} | |
server.start() | |
server.join() | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.segfault.playground.aswj | |
import com.google.inject.Inject | |
import com.google.inject.Singleton | |
import javax.ws.rs.GET | |
import javax.ws.rs.Path | |
import javax.ws.rs.Produces | |
import javax.ws.rs.core.MediaType | |
import javax.ws.rs.core.Response | |
@Singleton | |
class MyService { | |
@Inject constructor() | |
hello(): String = "Hello!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment