最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

創(chuà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的支持

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

一個簡單的應(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)建一個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文件增加以下設(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)建一個名為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文件增加以下依賴(由于測試功能我們使用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實體

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實現(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
 }

}

使用控制臺窗口執(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ù)(自行解析返回數(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)行測試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

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

相關(guān)文章

  • Spring 應(yīng)用上下文獲取 Bean 的常用姿勢實例總結(jié)

    Spring 應(yīng)用上下文獲取 Bean 的常用姿勢實例總結(jié)

    這篇文章主要介紹了Spring 應(yīng)用上下文獲取 Bean,結(jié)合實例形式總結(jié)分析了Spring 應(yīng)用上下文獲取 Bean的實現(xiàn)方法與操作注意事項,需要的朋友可以參考下
    2020-05-05
  • Disconf實現(xiàn)分布式配置管理的原理與設(shè)計

    Disconf實現(xiàn)分布式配置管理的原理與設(shè)計

    這篇文章主要為大家介紹了Disconf實現(xiàn)分布式配置管理的原理與設(shè)計分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java中BeanUtils.copyProperties基本用法與小坑

    Java中BeanUtils.copyProperties基本用法與小坑

    本文主要介紹了Java中BeanUtils.copyProperties基本用法與小坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 詳解SpringMVC學(xué)習(xí)系列之國際化

    詳解SpringMVC學(xué)習(xí)系列之國際化

    這篇文章主要介紹了詳解SpringMVC學(xué)習(xí)系列之國際化,詳細(xì)的介紹了關(guān)于瀏覽器,Session,Cookie,URL請求的國際化的實現(xiàn),有興趣的可以了解一下
    2017-07-07
  • Java之多個線程順序循環(huán)執(zhí)行的幾種實現(xiàn)

    Java之多個線程順序循環(huán)執(zhí)行的幾種實現(xiàn)

    這篇文章主要介紹了Java之多個線程順序循環(huán)執(zhí)行的幾種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java 內(nèi)省(Introspector)深入理解

    Java 內(nèi)省(Introspector)深入理解

    這篇文章主要介紹了Java 內(nèi)省(Introspector)深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • java 字符串分割的三種方法(總結(jié))

    java 字符串分割的三種方法(總結(jié))

    下面小編就為大家?guī)硪黄猨ava 字符串分割的三種方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • springboot + vue 實現(xiàn)遞歸生成多級菜單(實例代碼)

    springboot + vue 實現(xiàn)遞歸生成多級菜單(實例代碼)

    這篇文章主要介紹了springboot + vue 實現(xiàn)遞歸生成多級菜單,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Spring Boot下的Job定時任務(wù)

    Spring Boot下的Job定時任務(wù)

    編寫Job定時執(zhí)行任務(wù)十分有用,能解決很多問題,這次實習(xí)的項目里做了一下系統(tǒng)定時更新三方系統(tǒng)訂單狀態(tài)的功能,這里用到了Spring的定時任務(wù)使用的非常方便,下面總結(jié)一下如何使用,感興趣的朋友參考下吧
    2017-05-05
  • Mybatis之RowBounds分頁原理詳解

    Mybatis之RowBounds分頁原理詳解

    這篇文章主要介紹了Mybatis之RowBounds分頁原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評論

乐安县| 全州县| 海伦市| 綦江县| 陇川县| 临朐县| 循化| 西和县| 翁源县| 阿城市| 鹿泉市| 南华县| 朝阳市| 神农架林区| 彭阳县| 淮北市| 镇远县| 辉南县| 临湘市| 宜州市| 迁西县| 漳州市| 隆安县| 呈贡县| 长武县| 南开区| 稷山县| 海安县| 濉溪县| 柳林县| 鄂托克前旗| 石城县| 张家港市| 竹溪县| 泰宁县| 即墨市| 寻乌县| 孙吴县| 广平县| 黔西县| 紫阳县|