今天我们尝试Spring Boot整合Scala,并决定建立一个非常简单的Spring Boot微服务,使用Scala作为编程语言进行编码构建。
mvn archetype:generate -DgroupId=com.edurt.ssi -DartifactId=springboot-scala-integration -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.edurt.ssigroupId>
<artifactId>springboot-scala-integrationartifactId>
<packaging>jarpackaging>
<version>1.0.0version>
<name>springboot-scala-integrationname>
<description>SpringBoot Scala Integration is a open source springboot, scala integration example.description>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.3.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<dependency.scala.version>2.12.1dependency.scala.version>
<plugin.maven.scala.version>3.1.3plugin.maven.scala.version>
properties>
<dependencies>
<dependency>
<groupId>org.scala-langgroupId>
<artifactId>scala-libraryartifactId>
<version>${dependency.scala.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/scalasourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/scalatestSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.mavengroupId>
<artifactId>scala-maven-pluginartifactId>
<version>${plugin.maven.scala.version}version>
<executions>
<execution>
<goals>
<goal>compilegoal>
<goal>testCompilegoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.edurt.ssi
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class SpringBootScalaIntegration
object SpringBootScalaIntegration extends App{
SpringApplication.run(classOf[SpringBootScalaIntegration])
}
package com.edurt.ssi.controller
import org.springframework.web.bind.annotation.{GetMapping, RestController}
@RestController
class HelloController {
@GetMapping(value = Array("hello"))
def hello(): String = {
return "hello,scala"
}
}
@ComponentScan(value = Array(
"com.edurt.ssi.controller"
))
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mustacheartifactId>
dependency>
"com.edurt.ssi.view"
在src/main/resources路径下创建templates文件夹
在templates文件夹下创建一个名为hello.mustache的页面文件
<h1>Hello, Scalah1>
package com.edurt.ssi.view
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
@Controller
class HelloView {
@GetMapping(value = Array("hello_view"))
def helloView: String = {
return "hello";
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
@EntityScan(value = Array(
"com.edurt.ssi.model"
))
package com.edurt.ssi.model
import javax.persistence.{Entity, GeneratedValue, Id}
@Entity
class UserModel {
@Id
@GeneratedValue
var id: Long = 0
var name: String = null
}
package com.edurt.ssi.support
import com.edurt.ssi.model.UserModel
import org.springframework.data.repository.PagingAndSortingRepository
trait UserSupport extends PagingAndSortingRepository[UserModel, Long] {
}
package com.edurt.ssi.service
import com.edurt.ssi.model.UserModel
trait UserService {
/**
* save model to db
*/
def save(model: UserModel): UserModel;
}
package com.edurt.ssi.service
import com.edurt.ssi.model.UserModel
import com.edurt.ssi.support.UserSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service(value = "userService")
class UserServiceImpl @Autowired() (
val userSupport: UserSupport
) extends UserService {
/**
* save model to db
*/
override def save(model: UserModel): UserModel = {
return this.userSupport.save(model)
}
}
package com.edurt.ssi.controller
import com.edurt.ssi.model.UserModel
import com.edurt.ssi.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.{PathVariable, PostMapping, RequestMapping, RestController}
@RestController
@RequestMapping(value = Array("user"))
class UserController @Autowired()(
val userService: UserService
) {
@PostMapping(value = Array("save/{name}"))
def save(@PathVariable name: String): Long = {
val userModel = {
new UserModel()
}
userModel.name = name
return this.userService.save(userModel).id
}
}
curl -X POST http://localhost:8080/user/save/qianmoQ
收到返回结果
1
表示数据保存成功
/**
* get all model
*/
def getAll(page: Pageable): Page[UserModel]
/**
* get all model
*/
override def getAll(page: Pageable): Page[UserModel] = {
return this.userSupport.findAll(page)
}
@GetMapping(value = Array("list"))
def get(): Page[UserModel] = this.userService.getAll(PageRequest.of(0, 10))
package com.edurt.ssi.view
import com.edurt.ssi.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
@Controller
class UserView @Autowired()(
private val userService: UserService
) {
@GetMapping(value = Array("user_view"))
def helloView(model: Model): String = {
model.addAttribute("users", this.userService.getAll(PageRequest.of(0, 10)))
return "user"
}
}
{{users}}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>junitgroupId>
<artifactId>junitartifactId>
exclusion>
<exclusion>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<scope>testscope>
dependency>
package com.edurt.ssi
import com.edurt.ssi.service.UserService
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.domain.PageRequest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserServiceTest @Autowired()(
private val userService: UserService) {
@Test
def `get all`() {
println(">> Assert blog page title, content and status code")
val entity = this.userService.getAll(PageRequest.of(0, 1))
print(entity.getTotalPages)
}
}
源码地址:GitHub