SpringBoot整合Scala构建Web服务的方法

时间:2022-03-29 09:56:54

今天我们尝试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

 

修改pom.xml增加java和scala的支持

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<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>

一个简单的应用类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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输出信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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文件增加以下设置扫描路径

?
1
2
3
@componentscan(value = array(
 "com.edurt.ssi.controller"
))

添加页面功能

修改pom.xml文件增加以下页面依赖

?
1
2
3
4
5
<!-- mustache -->
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-mustache</artifactid>
</dependency>

修改springbootscalaintegration文件增加以下设置扫描路径componentscan的value字段中

?
1
"com.edurt.ssi.view"

在src/main/resources路径下创建templates文件夹

在templates文件夹下创建一个名为hello.mustache的页面文件

?
1
<h1>hello, scala</h1>

创建页面转换器helloview

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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文件增加以下依赖(由于测试功能我们使用h2内存数据库)

?
1
2
3
4
5
6
7
8
9
10
<!-- 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文件增加以下设置扫描model路径

?
1
2
3
@entityscan(value = array(
 "com.edurt.ssi.model"
))

创建user实体

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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数据库操作工具类

?
1
2
3
4
5
6
7
8
package com.edurt.ssi.support
 
import com.edurt.ssi.model.usermodel
import org.springframework.data.repository.pagingandsortingrepository
 
trait usersupport extends pagingandsortingrepository[usermodel, long] {
 
}

创建userservice服务类

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.edurt.ssi.service
 
import com.edurt.ssi.model.usermodel
 
trait userservice {
 
 /**
 * save model to db
 */
 def save(model: usermodel): usermodel;
 
}

创建userserviceimpl实现类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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进行持久化数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
 }
 
}

使用控制台窗口执行以下命令保存数据

?
1
curl -x post http://localhost:8080/user/save/qianmoq

收到返回结果

1

表示数据保存成功

增加数据读取渲染功能

修改userservice增加以下代码

?
1
2
3
4
/**
 * get all model
 */
def getall(page: pageable): page[usermodel]

修改userserviceimpl增加以下代码

?
1
2
3
4
5
6
/**
 * get all model
 */
override def getall(page: pageable): page[usermodel] = {
 return this.usersupport.findall(page)
}

修改usercontroller增加以下代码

?
1
2
@getmapping(value = array("list"))
def get(): page[usermodel] = this.userservice.getall(pagerequest.of(0, 10))

创建userview文件渲染user数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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文件渲染数据(自行解析返回数据即可)

?
1
{{users}}

浏览器访问http://localhost:8080/user_view即可看到页面内容

增加单元功能

修改pom.xml文件增加以下依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 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>

创建userservicetest文件进行测试userservice功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000018357979