Run Scala applications with Spring Boot through Gradle

This person's article Make a note of the result of the trial. The language is Scala, but it might be useful when you want to use Spring Boot.

Overview

  1. Add build.gradle
  2. Create a Scala object to start Spring Boot
  3. Create a Scala object for the Spring Boot controller

Add build.gradle

build.gradle


// Scala
apply plugin: 'scala'

dependencies {
    compile 'org.scala-lang:scala-library:2.12.6'
}

Creating a Scala object to start Spring Boot

Product code

ApplicationScala.scala


package hello

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

@SpringBootApplication
class ApplicationScala {}

object ApplicationScala {

  def main(args: Array[String]): Unit = SpringApplication.run(classOf[ApplicationScala], args: _*)

}

Test code

I'm still not sure if the test code will cover the main function that launches Spring Boot.

Creating a Scala object for the Spring Boot controller

Product code

ScalaController.scala


package hello

import org.springframework.web.bind.annotation.{RequestMapping, RequestMethod, RestController}

@RestController
@RequestMapping(Array("/scala"))
class ScalaController {

  @RequestMapping(method = Array(RequestMethod.GET))
  def sample = "sample from scala."

}

Test code

Using Spring Boot annotations, check the validity of the response as the result of API execution through URL mapping, not just the return value of the method.

ScalaControllerTest.scala


package hello

import org.hamcrest.Matchers.equalTo
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders

@RunWith(classOf[SpringRunner])
@SpringBootTest
@AutoConfigureMockMvc
class ScalaControllerTest {

  @Autowired
  val mvc: MockMvc = null

  @Test
  @throws[Exception]
  def sampleGet_Ok(): Unit =
    mvc.perform(MockMvcRequestBuilders.get("/scala").accept(MediaType.APPLICATION_JSON))
      .andExpect(status.isOk)
      .andExpect(content.string(equalTo("sample from scala.")))
}

Recommended Posts

Run Scala applications with Spring Boot through Gradle
Run LIFF with Spring Boot
Spring Boot gradle build with Docker
(IntelliJ + gradle) Hello World with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Create a website with Spring Boot + Gradle (jdk1.8.x)
Personal memo Run Spring Boot + Gradle web project with Codenvy (Eclipse Che)
Build Spring Boot project by environment with Gradle
I wanted to gradle spring boot with multi-project
Download with Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
[Java] Sample project for developing web applications with Spring Boot
Use Basic Authentication with Spring Boot
◆ Spring Boot + gradle environment construction memo
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Easily develop web applications with STS and Spring Boot. In 10 minutes.
Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Build Spring Boot + Docker image in Gradle
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
CICS-Run Java applications-(3) Build management with Gradle
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose