Scala + Spring Boot (Actuator RESTful API)


build.sbt

import sbt.internal.IvyConsole.Dependencies._

lazy val springVersion = "1.5.3.RELEASE"
lazy val thymeleafVersion = "2.1.5.RELEASE"

lazy val root = (project in file(".")).
settings(
inThisBuild(List(
organization := "org.bk",
scalaVersion := "2.11.0",
version := "1.0"
)),
name := "immosis-scheduler-immobilienscout24",
libraryDependencies += "org.springframework.boot" % "spring-boot-starter-web" % springVersion,
libraryDependencies += "org.springframework.boot" % "spring-boot-starter-actuator" % springVersion
)
mainClass in (Compile, run) := Some("HelloWorldApplication")

HelloWorldApplication.scala

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

object HelloWorldApplication extends App {
SpringApplication.run(classOf[HelloWorldApplication]);
}

@SpringBootApplication
class HelloWorldApplication

HelloWorldController.scala

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{GetMapping, RequestParam, ResponseBody}

@Controller
class HelloWorldController {

@GetMapping(Array("/hello-world"))
@ResponseBody
def sayHello(@RequestParam(name="name", required=false, defaultValue="Stranger") name: String): Greeting = {
new Greeting("Hello, "+name)
}
}

Greeting.scala
class Greeting(content: String){
def getContent():String = content
}

sbt run

curl localhost:8080/hello-world
{"content":"Hello, Stranger"}

Advertisements

Scala Parser for YAML File


import java.io.{File, FileInputStream}
import java.text.SimpleDateFormat
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor

object Main {
def main(args: Array[String]): Unit = {
val text = scala.io.Source.fromInputStream(getClass.getResourceAsStream(
"codes.yaml")).mkString
val yaml = new Yaml
val obj = yaml.load(text)
val codes = obj.asInstanceOf[java.util.LinkedHashMap[String,java.util.Map[String, Any]]]
var values = codes.get("duration").get("values").asInstanceOf[java.util.ArrayList
[java.util.Map[String,Any]]]
values.toArray.foreach(c=> println(c.asInstanceOf[java.util.LinkedHashMap[String,Any]]
.get("code")))
}
}

codes.yaml:

duration:
values:
-
code: 1
from: 0
to: 1
-
code: 2
from: 1
to: 3

build.sbt

libraryDependencies += "org.yaml" % "snakeyaml" % "1.8"

Hello World with Play

wget https://downloads.typesafe.com/typesafe-activator/1.3.6/typesafe-activator-1.3.6.zip?_ga=1.140741362.1365550810.1438956173

sudo unzip ~/Downloads/typesafe-activator-1.3.6.zip -d /opt/

export PATH=$PATH:/opt/activator-dist-1.3.6

java -version
java version “1.8.0_45”
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)

sudo chmod o+X /opt/activator-dist-1.3.6/activator

./activator new hello-world-app play-scala

cd hello-world-app

// in app/controllers/Application.scala
// change line
// Ok(views.html.index(“Your new application is ready.”)) -> Ok(“Hello World”)

./activator run

// in browser http://localhost:9000/

// to generate intillig idea project
mkdir -p ~/.sbt/0.13/plugins/
echo ‘addSbtPlugin(“com.github.mpeltonen” % “sbt-idea” % “1.6.0”)’ >>~/.sbt/0.13/plugins/build.sbt
./activator gen-idea