SpringBoot中使用纯scala进行开发 配置教程 非常简单的案例

  1. 新建项目

  2. 建好之后


    建一个叫scala的文件夹
    并把它标记为root文件夹
  3. 修改pom.xml文件
    复制粘贴如下内容:
    添加的插件的作用,如果不添加,在新建文件的时候右键只能新建java的文件,无法创建scala的class和object。添加插件之后就可以了。
<properties>
        <scala.version>2.11.8</scala.version>
        <spring.version>2.0.2.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <recompileMode>incremental</recompileMode>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <launchers>
                        <launcher>
                            <id>app</id>
                            <mainClass>com.light.sword.ylazy.LightSwordApplication</mainClass>
                            <args>
                                <arg>-deprecation</arg>
                            </args>
                            <jvmArgs>
                                <jvmArg>-Xms256m</jvmArg>
                                <jvmArg>-Xmx2048m</jvmArg>
                            </jvmArgs>
                        </launcher>
                    </launchers>
                </configuration>
                <dependencies>
                    <!-- spring热部署-->
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.6.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. 在scala(文件夹)下新建一个包,按照下图的层级关系,建好各个文件。

  1. 各个文件的内容
package xxproj

import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class AppConfig {

}

package xxproj

import org.springframework.boot.SpringApplication

object xxprojApplication extends App {
  SpringApplication.run(classOf[AppConfig])
}

package xxproj.controller

import com.alibaba.fastjson.JSONObject
import org.springframework.web.bind.annotation.{RequestMapping, RestController}

@RestController
class indexController {

  @RequestMapping(value = Array("/index"))
  def index(): JSONObject = {
    val json = new JSONObject
    json.put("code", 0)
    json.put("data", "success")
    json
  }
}

  1. 运行xxprojApplication
    如果没报错就可以正常访问了

  2. 浏览器输入localhost:8080/index

欢迎关注微信公众号“ IT客“ ,投稿邮箱 itkeyy@163.com

posted @ 2018-06-14 16:18  须小弥  阅读(9460)  评论(2编辑  收藏  举报