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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

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

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

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

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

創(chuàng)建項目

初始化項目

 

復制代碼 代碼如下:
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接口功能

創(chuàng)建一個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路徑下創(chuàng)建templates文件夾

在templates文件夾下創(chuàng)建一個名為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文件增加以下設置掃描model路徑

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

創(chuàng)建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
 
}

創(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服務類

?
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實現(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進行持久化數(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ù)(自行解析返回數(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文件進行測試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)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 我的绝色岳每雯雯 | 校园情射 | 国产亚洲欧美日韩俺去了 | a毛片免费全部在线播放毛 a级在线看 | 亚洲一区二区福利视频 | 亚洲国产精品久久久久久 | 大象传媒短视频网站 | 久久人妻无码毛片A片麻豆 久久热这里只有 精品 | 香蕉久久一区二区三区啪啪 | 色狠狠婷婷97 | 久久天天躁狠狠躁夜夜躁 | 精品国产欧美一区二区 | h黑寡妇一级毛片 | 欧美性色欧美a在线播放 | 91韩国女主播 | 久久国产影院 | 4虎影院永久地址www | 双龙高h| 久久99国产综合精品AV蜜桃 | 精品国产午夜久久久久九九 | 香蕉eeww99国产精品 | 91亚洲专区 | 欧美极品摘花过程 | 男生同性视频twink在线 | 国产草草视频 | 桥本有菜作品在线 | 俺去啦最新官网 | 小柔的性放荡羞辱日记 | 无码一区国产欧美在线资源 | 国产综合久久 | 青草视频免费 | 美女被免费视频 | 成人欧美一区二区三区 | 亚洲国产成人久久77 | 美国女网址www呦女 美国复古性经典xxxxx | 国产精品1| 免费网站看v片在线成人国产系列 | 好大好深受不了了快进来 | 国产男女乱淫真视频全程播放 | 小早川怜子息梦精在线播放 | 国产激情视频 |