DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot With Scala

Spring Boot With Scala

An updated look at a Scala app using the latest version of Spring Boot and some of the supporting libraries.

Biju Kunjummen user avatar by
Biju Kunjummen
·
Feb. 26, 16 · Tutorial
Like (6)
Save
Tweet
Share
19.92K Views

Join the DZone community and get the full member experience.

Join For Free

A while back I had tried out a small Spring Boot  based sample application with Scala as the language and found that the combination works out quite nicely — no big surprises there actually as Scala programs ultimately run in the JVM. I have now updated the sample with the latest version of Spring Boot and some of the supporting libraries.

To very quickly revisit the sample, it is a very simple web application with a UI to manage a "Hotel" domain object managed via JPA, represented in scala the following way:

import javax.persistence.Id
import javax.persistence.GeneratedValue
import java.lang.Long
import javax.persistence.Entity
import scala.beans.BeanProperty
import org.hibernate.validator.constraints.NotEmpty

@Entity
class Hotel {

 @Id
 @GeneratedValue
 @BeanProperty
 var id: Long = _

 @BeanProperty
 @NotEmpty
 var name: String = _

 @BeanProperty
 @NotEmpty
 var address: String = _

 @BeanProperty
 @NotEmpty
 var zip: String = _
}

JPA annotations carry over quite well, one wrinkle may be the additional @BeanProperty annotation though, this is required for JPA implementations as this makes the scala compiler generate the normal Java Beans type getters and setters instead of the scala default getters and setters which don't follow the Java Bean conventions.

Spring Data  makes it ridiculously simple to manage this domain type, all it requires is a marker interface and it generates a runtime implementation:

import org.springframework.data.repository.CrudRepository
import mvctest.domain.Hotel
import java.lang.Long

trait HotelRepository extends CrudRepository[Hotel, Long]

Now I have a toolkit available for managing the Hotel domain:

//save or update a hotel
hotelRepository.save(hotel)

//find one hotel
hotelRepository.findOne(id)

//find all hotels
val hotels = hotelRepository.findAll()

//delete a hotel
hotelRepository.delete(id)

And finally a controller to manage the UI flow with this repository:

@Controller
@RequestMapping(Array("/hotels"))
class HotelController @Autowired()(private val hotelRepository: HotelRepository) {

 @RequestMapping(method = Array(RequestMethod.GET))
 def list(model: Model) = {
 val hotels = hotelRepository.findAll()
 model.addAttribute("hotels", hotels)
 "hotels/list"
 }

 @RequestMapping(Array("/edit/{id}"))
 def edit(@PathVariable("id") id: Long, model: Model) = {
 model.addAttribute("hotel", hotelRepository.findOne(id))
 "hotels/edit"
 }

 @RequestMapping(method = Array(RequestMethod.GET), params = Array("form"))
 def createForm(model: Model) = {
 model.addAttribute("hotel", new Hotel())
 "hotels/create"
 }

 @RequestMapping(method = Array(RequestMethod.POST))
 def create(@Valid hotel: Hotel, bindingResult: BindingResult) = {
 if (bindingResult.hasErrors()) {
 "hotels/create"
 } else {
 hotelRepository.save(hotel)
 "redirect:/hotels"
 }
 }

 @RequestMapping(value = Array("/update"), method = Array(RequestMethod.POST))
 def update(@Valid hotel: Hotel, bindingResult: BindingResult) = {
 if (bindingResult.hasErrors()) {
 "hotels/edit"
 } else {
 hotelRepository.save(hotel)
 "redirect:/hotels"
 }
 }

 @RequestMapping(value = Array("/delete/{id}"))
 def delete(@PathVariable("id") id: Long) = {
 hotelRepository.delete(id)
 "redirect:/hotels"
 }
}

There are some wrinkles here too but should mostly make sense, the way the repository is autowired is a little non-intuitive and the way an explicit Array type has to be provided for request mapping paths and methods may be confusing.

Beyond these small concerns the code just works, do play with it, I would love any feedback on ways to improve this sample. Here is the git location of this sample.

Spring Framework Spring Boot Scala (programming language)

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Memory Debugging: A Deep Level of Insight
  • What Should You Know About Graph Database’s Scalability?
  • Cloud-Native Application Networking
  • Top 5 PHP REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: