一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot整合Scala構(gòu)建Web服務(wù)的方法

SpringBoot整合Scala構(gòu)建Web服務(wù)的方法

2021-07-18 15:52qianmoQ Java教程

這篇文章主要介紹了SpringBoot整合Scala構(gòu)建Web服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

今天我們嘗試spring boot整合scala,并決定建立一個(gè)非常簡單的spring boot微服務(wù),使用scala作為編程語言進(jìn)行編碼構(gòu)建。

創(chuàng)建項(xiàng)目

初始化項(xiàng)目

 

復(fù)制代碼 代碼如下:
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>

一個(gè)簡單的應(yīng)用類

?
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接口功能

創(chuàng)建一個(gè)hellocontroller rest api接口,我們只提供一個(gè)簡單的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文件增加以下設(shè)置掃描路徑

?
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文件增加以下設(shè)置掃描路徑componentscan的value字段中

?
1
"com.edurt.ssi.view"

在src/main/resources路徑下創(chuàng)建templates文件夾

在templates文件夾下創(chuàng)建一個(gè)名為hello.mustache的頁面文件

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

創(chuàng)建頁面轉(zhuǎn)換器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即可看到頁面內(nèi)容

添加數(shù)據(jù)持久化功能

修改pom.xml文件增加以下依賴(由于測試功能我們使用h2內(nèi)存數(shù)據(jù)庫)

?
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文件增加以下設(shè)置掃描model路徑

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

創(chuàng)建user實(shí)體

?
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
 
}

創(chuàng)建usersupport dao數(shù)據(jù)庫操作工具類

?
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] {
 
}

創(chuàng)建userservice服務(wù)類

?
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;
 
}

創(chuàng)建userserviceimpl實(shí)現(xiàn)類

?
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)
 }
 
}

創(chuàng)建用戶usercontroller進(jìn)行持久化數(shù)據(jù)

?
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
 }
 
}

使用控制臺窗口執(zhí)行以下命令保存數(shù)據(jù)

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

收到返回結(jié)果

1

表示數(shù)據(jù)保存成功

增加數(shù)據(jù)讀取渲染功能

修改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))

創(chuàng)建userview文件渲染user數(shù)據(jù)

?
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"
 }
 
}

創(chuàng)建user.mustache文件渲染數(shù)據(jù)(自行解析返回?cái)?shù)據(jù)即可)

?
1
{{users}}

瀏覽器訪問http://localhost:8080/user_view即可看到頁面內(nèi)容

增加單元功能

修改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>

創(chuàng)建userservicetest文件進(jìn)行測試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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草国产精品久久碰 | 国产欧美综合一区二区 | 午夜爱 | 96萝莉| 精品国产免费第一区二区 | 99久久精品免费看国产一区 | 公妇乱淫 | 男人猛进女人屁股免费 | 亚洲视频中文字幕 | 国产有码在线 | 大妹子最新视频在线观看 | 免费看60分钟大片视频播放 | 网红思瑞一区二区三区 | 国产精品亚欧美一区二区三区 | 处女摘花视频 | 国产精品永久免费视频 | 波多野结衣作品在线观看 | 天码毛片一区二区三区入口 | 欧美日韩国产一区二区三区欧 | 香蕉国产人午夜视频在线观看 | 免费观看二十女人一摸是水 | 污污的动态图合集 | 嫩草影院国产 | 任你操视频在线观看 | 国产精品成人自拍 | 午夜无码片在线观看影院 | 欧美色成人tv在线播放 | 国内精品自产拍在线观看91 | 久久精品视在线观看85 | 国产麻豆精品原创 | 爆操萝莉 | 精品久久香蕉国产线看观看麻豆 | 无套内谢大学生A片 | 国产一二在线观看视频网站 | 亚洲国产欧美在线人成aaaa20 | pron在线观看| 男人天堂官方网站 | 免费又爽又黄禁片视频在线播放 | 国产精品日韩欧美一区二区 | haodiaose在线精品免费视频 | 欧美亚洲另类在线观看 |