<dependency> <groupId>org.scalatra</groupId> <artifactId>scalatra_2.12</artifactId> <version>2.8.2</version> </dependency> import org.scalatra.ScalatraServlet; import org.json.JSONObject; public class MyJsonService extends ScalatraServlet { get("/hello") { val json = new JSONObject() json.put("message", "Hello World!") json.toString() } post("/hello") { val requestBody = parse(request.body) val name = (requestBody \ "name").extract[String] val json = new JSONObject() json.put("message", s"Hello, $name!") json.toString() } } import org.scalatra._ import javax.servlet.ServletContext class ScalatraBootstrap extends LifeCycle { override def init(context: ServletContext) { context.mount(new MyJsonService, "/*") } } import org.eclipse.jetty.server.Server import org.eclipse.jetty.webapp.WebAppContext object JettyLauncher { def main(args: Array[String]) { val server = new Server(8080) val context = new WebAppContext() context.setResourceBase(".") context.setContextPath("/") context.addServlet(classOf[ScalatraBootstrap], "/*") server.setHandler(context) server.start() server.join() } }


上一篇:
下一篇:
切换中文