The scala is very popular language now days specially while work with java bigdata technology. Here is example o Spring boot scala example code with details, Here we have used scala 2.12.1, Spring Boot,Java-8, Maven.

Spring boot scala project

spring boot scala example

spring boot scala example

pom.xml

scala-library-2.12.1 contains code packages of scala.

net.alchim31.maven plugin has been used for scala jar packaging.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javadeveloperzone.scala</groupId>
    <artifactId>spring-boot-scala-example</artifactId>
    <description>This article contains spring boot scala example</description>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <scala.version>2.12.1</scala.version>       <!-- scala version -->
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>           <!-- It contains scala dependency -->
            <artifactId>scala-library</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.3</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>

ScalaDemoController.scala

package com.javadeveloperzone.controller

import org.springframework.web.bind.annotation.{GetMapping, RestController}


/**
  * Created by Java Developer Zone on 12-11-2017.
  * This class will handle requires and code has been write in scala
  */
@RestController
class ScalaDemoController {

    @GetMapping(path = Array("/demo"))
    def demo={
      "This is scala first spring boot application."
    }
}

Application.scala

package com.javadeveloperzone

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

/**
  * Created by Java Developer Zone on 12-11-2017.
  */
@SpringBootApplication
class Application

object Application extends App {
  SpringApplication.run(classOf[Application]);
}

Create JAR File

mvn install command to create jar file of scala application.

mvn install

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-scala-example 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-scala-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-scala-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- scala-maven-plugin:3.1.3:compile (default) @ spring-boot-scala-example ---
[INFO] F:\extrawork\demo\src\main\scala:-1: info: compiling
[INFO] Compiling 2 source files to F:\extrawork\demo\target\classes at 1510498111040
[INFO] prepare-compile in 0 s
[INFO] compile in 4 s
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-scala-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\extrawork\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-scala-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- scala-maven-plugin:3.1.3:testCompile (default) @ spring-boot-scala-example ---
[WARNING] No source files found.
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-scala-example ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ spring-boot-scala-example ---
[INFO] Building jar: F:\extrawork\demo\target\spring-boot-scala-example-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ spring-boot-scala-example ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ spring-boot-scala-example ---
[INFO] Installing F:\extrawork\demo\target\spring-boot-scala-example-1.0-SNAPSHOT.jar to C:\Users\Lenovo\.m2\r
epository\com\javadeveloperzone\scala\spring-boot-scala-example\1.0-SNAPSHOT\spring-boot-scala-example-1.0-SNA
PSHOT.jar
[INFO] Installing F:\extrawork\demo\pom.xml to C:\Users\Lenovo\.m2\repository\com\javadeveloperzone\scala\spri
ng-boot-scala-example\1.0-SNAPSHOT\spring-boot-scala-example-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.469 s
[INFO] Finished at: 2017-11-12T20:18:38+05:30
[INFO] Final Memory: 25M/418M
[INFO] ------------------------------------------------------------------------

Start Spring Boot scala application:

java -jar spring-boot-scala-example-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-11-12 20:22:07.726  INFO 95776 --- [           main] scala.App                                : Starting App on Mahesh with PID 95776 (C:\Users\Lenovo\.ivy2\cache\org.scala-lang\scala-library\jars\scala-library-2.12.1.jar started by Lenovo in F:\extrawork\demo)
2017-11-12 20:22:07.726  INFO 95776 --- [           main] scala.App                                : No active profile set, falling back to default profiles: default
2017-11-12 20:22:07.866  INFO 95776 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]17f9d882: startup date [Sun Nov 12 20:22:07 IST 2017]; root of context hierarchy
2017-11-12 20:22:11.359  INFO 95776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-11-12 20:22:11.375  INFO 95776 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-11-12 20:22:11.390  INFO 95776 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-11-12 20:22:11.609  INFO 95776 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-11-12 20:22:11.609  INFO 95776 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3758 ms
2017-11-12 20:22:11.875  INFO 95776 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-12 20:22:11.875  INFO 95776 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-12 20:22:11.875  INFO 95776 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-12 20:22:11.875  INFO 95776 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-12 20:22:11.875  INFO 95776 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-12 20:22:12.578  INFO 95776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]17f9d882: startup date [Sun Nov 12 20:22:07 IST 2017]; root of context hierarchy
2017-11-12 20:22:12.805  INFO 95776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo],methods=[GET]}" onto public java.lang.String com.javadeveloperzone.controller.ScalaDemoController.demo()
2017-11-12 20:22:12.817  INFO 95776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-11-12 20:22:12.818  INFO 95776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-11-12 20:22:12.872  INFO 95776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-12 20:22:12.872  INFO 95776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-12 20:22:12.944  INFO 95776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-12 20:22:13.342  INFO 95776 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-11-12 20:22:13.468  INFO 95776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-11-12 20:22:13.475  INFO 95776 --- [           main] scala.App                                : Started App in 6.655 seconds (JVM running for 7.852)

Output:

http://localhost:8080/demo

Spring boot scala example - output

Spring boot scala example – output

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *