欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-28 08:59:04
今天我们尝试spring boot整合scala,并决定建立一个非常简单的spring boot微服务,使用scala作为编程语言进行编码构建。 创建项目 初始化项...

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

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

一个简单的应用类

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文件增加以下页面依赖

<!-- mustache -->
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-mustache</artifactid>
</dependency>

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

"com.edurt.ssi.view"

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

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

<h1>hello, scala</h1>

创建页面转换器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文件增加以下依赖(由于测试功能我们使用h2内存数据库)

<!-- 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路径

@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文件增加以下依赖

<!-- 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功能

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

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