SpringBoot整合Scala構(gòu)建Web服務(wù)的方法
今天我們嘗試Spring Boot整合Scala,并決定建立一個(gè)非常簡(jiǎn)單的Spring Boot微服務(wù),使用Scala作為編程語言進(jìn)行編碼構(gòu)建。
創(chuàng)建項(xiàng)目
初始化項(xiàng)目
修改pom.xml增加java和scala的支持
<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.0</modelVersion>
<groupId>com.edurt.ssi</groupId>
<artifactId>springboot-scala-integration</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>springboot-scala-integration</name>
<description>SpringBoot Scala Integration is a open source springboot, scala integration example.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- dependency config -->
<dependency.scala.version>2.12.1</dependency.scala.version>
<!-- plugin config -->
<plugin.maven.scala.version>3.1.3</plugin.maven.scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${dependency.scala.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/scala</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${plugin.maven.scala.version}</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>
</project>
一個(gè)簡(jiǎn)單的應(yīng)用類
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])
}
添加Rest API接口功能
創(chuàng)建一個(gè)HelloController Rest API接口,我們只提供一個(gè)簡(jiǎn)單的get請(qǐng)求獲取hello,scala輸出信息
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"
}
}
修改SpringBootScalaIntegration文件增加以下設(shè)置掃描路徑
@ComponentScan(value = Array( "com.edurt.ssi.controller" ))
添加頁面功能
修改pom.xml文件增加以下頁面依賴
<!-- mustache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mustache</artifactId> </dependency>
修改SpringBootScalaIntegration文件增加以下設(shè)置掃描路徑ComponentScan的value字段中
"com.edurt.ssi.view"
在src/main/resources路徑下創(chuàng)建templates文件夾
在templates文件夾下創(chuàng)建一個(gè)名為hello.mustache的頁面文件
<h1>Hello, Scala</h1>
創(chuàng)建頁面轉(zhuǎn)換器HelloView
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";
}
}
瀏覽器訪問http://localhost:8080/hello_view即可看到頁面內(nèi)容
添加數(shù)據(jù)持久化功能
修改pom.xml文件增加以下依賴(由于測(cè)試功能我們使用h2內(nèi)存數(shù)據(jù)庫)
<!-- data jpa and db --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
修改SpringBootScalaIntegration文件增加以下設(shè)置掃描model路徑
@EntityScan(value = Array( "com.edurt.ssi.model" ))
創(chuàng)建User實(shí)體
package com.edurt.ssi.model
import javax.persistence.{Entity, GeneratedValue, Id}
@Entity
class UserModel {
@Id
@GeneratedValue
var id: Long = 0
var name: String = null
}
創(chuàng)建UserSupport dao數(shù)據(jù)庫操作工具類
package com.edurt.ssi.support
import com.edurt.ssi.model.UserModel
import org.springframework.data.repository.PagingAndSortingRepository
trait UserSupport extends PagingAndSortingRepository[UserModel, Long] {
}
創(chuàng)建UserService服務(wù)類
package com.edurt.ssi.service
import com.edurt.ssi.model.UserModel
trait UserService {
/**
* save model to db
*/
def save(model: UserModel): UserModel;
}
創(chuàng)建UserServiceImpl實(shí)現(xiàn)類
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)
}
}
創(chuàng)建用戶UserController進(jìn)行持久化數(shù)據(jù)
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
}
}
使用控制臺(tái)窗口執(zhí)行以下命令保存數(shù)據(jù)
curl -X POST http://localhost:8080/user/save/qianmoQ
收到返回結(jié)果
1
表示數(shù)據(jù)保存成功
增加數(shù)據(jù)讀取渲染功能
修改UserService增加以下代碼
/** * get all model */ def getAll(page: Pageable): Page[UserModel]
修改UserServiceImpl增加以下代碼
/**
* get all model
*/
override def getAll(page: Pageable): Page[UserModel] = {
return this.userSupport.findAll(page)
}
修改UserController增加以下代碼
@GetMapping(value = Array("list"))
def get(): Page[UserModel] = this.userService.getAll(PageRequest.of(0, 10))
創(chuàng)建UserView文件渲染User數(shù)據(jù)
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"
}
}
創(chuàng)建user.mustache文件渲染數(shù)據(jù)(自行解析返回?cái)?shù)據(jù)即可)
{{users}}
瀏覽器訪問http://localhost:8080/user_view即可看到頁面內(nèi)容
增加單元功能
修改pom.xml文件增加以下依賴
<!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency>
創(chuàng)建UserServiceTest文件進(jìn)行測(cè)試UserService功能
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程池運(yùn)行狀態(tài)監(jiān)控實(shí)現(xiàn)解析
這篇文章主要介紹了Java線程池運(yùn)行狀態(tài)監(jiān)控實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
SpringBoot實(shí)現(xiàn)Mysql使用MD5進(jìn)行密碼加密的示例
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)Mysql使用MD5進(jìn)行密碼加密的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
java線程安全鎖ReentrantReadWriteLock原理分析readLock
這篇文章主要為大家介紹了java線程安全鎖ReentrantReadWriteLock原理分析readLock,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java實(shí)現(xiàn)XML文件學(xué)生通訊錄
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)XML文件學(xué)生通訊錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Struts2學(xué)習(xí)筆記(3)-DMI動(dòng)態(tài)調(diào)用方式
本文主要介紹Struts2的DMI動(dòng)態(tài)調(diào)用的兩種方式,簡(jiǎn)單實(shí)用,希望能給大家做一個(gè)參考。2016-06-06
一文帶你看懂Java8中的lambda表達(dá)式和方法引用
Lambda 表達(dá)式是 Java 8 引入的一項(xiàng)重要特性,它提供了一種簡(jiǎn)潔、清晰且靈活的語法來表示可傳遞的匿名函數(shù),下面就跟隨小編一起學(xué)習(xí)一下Java8中的lambda表達(dá)式和方法引用的相關(guān)知識(shí)吧2023-12-12
Java 超詳細(xì)講解IO操作字節(jié)流與字符流
本章具體介紹了字節(jié)流、字符流的基本使用方法,圖解穿插代碼實(shí)現(xiàn)。 JAVA從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03最新評(píng)論

