Hello World Scala in the Cloud With Spring
In this tutorial, we are going to look at Scala using Spring MVC and MongoDB.
The first step is a Maven project and adds the following content to your Maven POM file. The easiest way to bootstrap a Spring Boot Maven project is by using Spring Initializer.
The next step is to set the Scala dependency in the pom.xml:
<dependencies>
...
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
<build>
<finalName>spring-scala-mongodb</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.4.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you want to run MongoDB locally, a good option might be Docker, which you can run with the command below:
xxxxxxxxxx
docker run -d --name mongodb-instance -p 27017:27017 mongo
In this project, we’ll create a sample that will handle a user in its respective client. The first step is to create the entity class, in this sample, a User class.
xxxxxxxxxx
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import scala.annotation.meta.field
import scala.beans.BeanProperty
class User
(Id ) var id: String, (
var name: String,
var country: String) {
def this() = this(null, null, null)
}
The repository interface makes the integration between the Scala application and the MongoDB instance easy:
xxxxxxxxxx
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
trait UserRepository extends CrudRepository[User, String]
The last step is to create a resource where the client can do the request and then the CRUD:
xxxxxxxxxx
import javax.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.{GetMapping, PathVariable, PostMapping}
class UserController ()(private val userRepository: UserRepository){
Array("/")) def start(model: Model): String = { (
model.addAttribute("users", userRepository.findAll)
"index"
}
Array("/signup")) def showSignUpForm(user: User) = "add-user" (
Array("/adduser")) def addUser( user: User, result: BindingResult, model: Model): String = { (
if (result.hasErrors) return "add-user"
userRepository.save(user)
model.addAttribute("users", userRepository.findAll)
"index"
}
Array("/edit/{id}")) def showUpdateForm( ("id") id: String, model: Model): String = { (
val user = userRepository.findById(id).orElseThrow(() => new IllegalArgumentException("Invalid user Id:" + id))
model.addAttribute("user", user)
"update-user"
}
Array("/update/{id}")) def updateUser( ("id") id: String, user: User, result: BindingResult, model: Model): String = { (
if (result.hasErrors) {
user.setId(id)
return "update-user"
}
userRepository.save(user)
model.addAttribute("users", userRepository.findAll)
"index"
}
Array("/delete/{id}")) def deleteUser( ("id") id: String, model: Model): String = { (
val user = userRepository.findById(id).orElseThrow(() => new IllegalArgumentException("Invalid user Id:" + id))
userRepository.delete(user)
model.addAttribute("users", userRepository.findAll)
"index"
}
}
To run the Java application left the startup class, where we define it as a boot app.
xxxxxxxxxx
import org.springframework.boot.autoconfigure.SpringBootApplication
class BootConfig
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.SpringApplication
object Application extends App {
SpringApplication.run(classOf[BootConfig]);
}
In the front-end site thymeleaf parses the HTML templates and evaluates all expressions to render the value that was set in the controller: