今天就跟大家聊聊有关怎么在SpringBoot中利用Scala构建一个Web服务,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

十年专注成都网站制作,企业网站建设,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站建设,高端网页制作,对成都服务器托管等多个行业,拥有多年的网站营销经验。
创建项目
初始化项目
mvn archetype:generate -DgroupId=com.edurt.ssi -DartifactId=springboot-scala-integration -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
修改pom.xml增加java和scala的支持
4.0.0 com.edurt.ssi springboot-scala-integration jar 1.0.0 springboot-scala-integration SpringBoot Scala Integration is a open source springboot, scala integration example. org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE UTF-8 UTF-8 1.8 1.8 1.8 2.12.1 3.1.3 org.scala-lang scala-library ${dependency.scala.version} org.springframework.boot spring-boot-starter-web ${project.basedir}/src/main/scala ${project.basedir}/src/test/scala net.alchim31.maven scala-maven-plugin ${plugin.maven.scala.version} compile testCompile org.springframework.boot spring-boot-maven-plugin
一个简单的应用类
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接口功能
创建一个HelloController Rest API接口,我们只提供一个简单的get请求获取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文件增加以下设置扫描路径
@ComponentScan(value = Array( "com.edurt.ssi.controller" ))
添加页面功能
修改pom.xml文件增加以下页面依赖
org.springframework.boot spring-boot-starter-mustache
修改SpringBootScalaIntegration文件增加以下设置扫描路径ComponentScan的value字段中
"com.edurt.ssi.view"
在src/main/resources路径下创建templates文件夹
在templates文件夹下创建一个名为hello.mustache的页面文件
Hello, Scala
创建页面转换器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即可看到页面内容
添加数据持久化功能
修改pom.xml文件增加以下依赖(由于测试功能我们使用h3内存数据库)
org.springframework.boot spring-boot-starter-data-jpa com.h3database h3 runtime
修改SpringBootScalaIntegration文件增加以下设置扫描model路径
@EntityScan(value = Array( "com.edurt.ssi.model" ))
创建User实体
package com.edurt.ssi.model
import javax.persistence.{Entity, GeneratedValue, Id}
@Entity
class UserModel {
@Id
@GeneratedValue
var id: Long = 0
var name: String = null
}创建UserSupport dao数据库操作工具类
package com.edurt.ssi.support
import com.edurt.ssi.model.UserModel
import org.springframework.data.repository.PagingAndSortingRepository
trait UserSupport extends PagingAndSortingRepository[UserModel, Long] {
}创建UserService服务类
package com.edurt.ssi.service
import com.edurt.ssi.model.UserModel
trait UserService {
/**
* save model to db
*/
def save(model: UserModel): UserModel;
}创建UserServiceImpl实现类
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)
}
}创建用户UserController进行持久化数据
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
表示数据保存成功
增加数据读取渲染功能
修改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))创建UserView文件渲染User数据
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"
}
}创建user.mustache文件渲染数据(自行解析返回数据即可)
{{users}}浏览器访问http://localhost:8080/user_view即可看到页面内容
增加单元功能
修改pom.xml文件增加以下依赖
org.springframework.boot spring-boot-starter-test test junit junit org.mockito mockito-core org.junit.jupiter junit-jupiter-engine test
创建UserServiceTest文件进行测试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)
}
}springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
看完上述内容,你们对怎么在SpringBoot中利用Scala构建一个Web服务有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
名称栏目:怎么在SpringBoot中利用Scala构建一个Web服务
转载源于:http://www.jxjierui.cn/article/jejddd.html


咨询
建站咨询
