Skip to content

Commit

Permalink
Migrate from Spring MVC to Spring Webflux
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Dec 1, 2017
1 parent 786e776 commit 279b2e7
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 223 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Expand Up @@ -9,6 +9,7 @@ buildscript {
extra["boostrapVersion"] = "3.3.6"
extra["jQueryVersion"] = "2.2.4"
extra["jQueryUIVersion"] = "1.11.4"
extra["elVersion"] = "3.0.1-b08"

val springBootVersion: String by extra

Expand Down Expand Up @@ -43,6 +44,7 @@ val jUnitVersion: String by extra
val boostrapVersion: String by extra
val jQueryVersion: String by extra
val jQueryUIVersion: String by extra
val elVersion: String by extra

version = springBootVersion

Expand All @@ -64,7 +66,7 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-cache")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-webflux")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("javax.cache:cache-api")
compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
Expand All @@ -75,6 +77,7 @@ dependencies {
compile("org.webjars:bootstrap:$boostrapVersion")

testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.glassfish:javax.el:$elVersion")
testCompile("org.junit.jupiter:junit-jupiter-api:$jUnitVersion")
testRuntime("org.junit.jupiter:junit-jupiter-engine:$jUnitVersion")

Expand Down
Expand Up @@ -24,7 +24,6 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.InitBinder
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.servlet.ModelAndView
import javax.validation.Valid

/**
Expand Down Expand Up @@ -114,10 +113,9 @@ class OwnerController(val owners: OwnerRepository) {
* @return a ModelMap with the model attributes for the view
*/
@GetMapping("/owners/{ownerId}")
fun showOwner(@PathVariable("ownerId") ownerId: Int): ModelAndView {
val mav = ModelAndView("owners/ownerDetails")
mav.addObject(this.owners.findById(ownerId))
return mav
fun showOwner(@PathVariable("ownerId") ownerId: Int, model: Model): String {
model.addAttribute(this.owners.findById(ownerId))
return "owners/ownerDetails"
}

}
Expand Down
Expand Up @@ -17,7 +17,7 @@ package org.springframework.samples.petclinic.owner


import org.springframework.stereotype.Controller
import org.springframework.ui.ModelMap
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.validation.BindingResult
import org.springframework.web.bind.WebDataBinder
Expand Down Expand Up @@ -54,21 +54,21 @@ class PetController(val pets: PetRepository, val owners: OwnerRepository) {
}

@GetMapping(value = "/pets/new")
fun initCreationForm(owner: Owner, model: ModelMap): String {
fun initCreationForm(owner: Owner, model: Model): String {
val pet = Pet()
owner.addPet(pet)
model.put("pet", pet)
model.addAttribute("pet", pet)
return VIEWS_PETS_CREATE_OR_UPDATE_FORM
}

@PostMapping(value = "/pets/new")
fun processCreationForm(owner: Owner, @Valid pet: Pet, result: BindingResult, model: ModelMap): String {
fun processCreationForm(owner: Owner, @Valid pet: Pet, result: BindingResult, model: Model): String {
if (StringUtils.hasLength(pet.name) && pet.isNew && owner.getPet(pet.name!!, true) != null) {
result.rejectValue("name", "duplicate", "already exists")
}
owner.addPet(pet)
return if (result.hasErrors()) {
model.put("pet", pet)
model.addAttribute("pet", pet)
VIEWS_PETS_CREATE_OR_UPDATE_FORM
} else {
this.pets.save(pet)
Expand All @@ -77,17 +77,17 @@ class PetController(val pets: PetRepository, val owners: OwnerRepository) {
}

@GetMapping(value = "/pets/{petId}/edit")
fun initUpdateForm(@PathVariable petId: Int, model: ModelMap): String {
fun initUpdateForm(@PathVariable petId: Int, model: Model): String {
val pet = pets.findById(petId)
model.put("pet", pet)
model.addAttribute("pet", pet)
return VIEWS_PETS_CREATE_OR_UPDATE_FORM
}

@PostMapping(value = "/pets/{petId}/edit")
fun processUpdateForm(@Valid pet: Pet, result: BindingResult, owner: Owner, model: ModelMap): String {
fun processUpdateForm(@Valid pet: Pet, result: BindingResult, owner: Owner, model: Model): String {
return if (result.hasErrors()) {
pet.owner = owner
model.put("pet", pet)
model.addAttribute("pet", pet)
VIEWS_PETS_CREATE_OR_UPDATE_FORM
} else {
owner.addPet(pet)
Expand Down
Expand Up @@ -24,7 +24,7 @@ import org.springframework.web.bind.annotation.GetMapping
* @author Michael Isvy
* @author Antoine Rey
* <p/>
* Also see how a view that resolves to "error" has been added ("error.html").
* Also see how a view that resolves to "5xx" has been added ("5xx.html").
*/
@Controller
class CrashController {
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/templates/error.html

This file was deleted.

11 changes: 11 additions & 0 deletions src/main/resources/templates/error/5xx.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'error')}">

<body>
<img src="../../static/resources/images/pets.png" th:src="@{/resources/images/pets.png}"/>
<h2>Something happened...</h2>
<p th:text="${message}">Exception message</p>
</body>

</html>
12 changes: 6 additions & 6 deletions src/main/resources/templates/fragments/layout.html
Expand Up @@ -4,11 +4,11 @@
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link rel="shortcut icon" type="image/x-icon" th:href="@{/resources/images/favicon.png}">
<link rel="shortcut icon" type="image/x-icon" th:href="@{/resources/images/favicon.png}" />

<title>PetClinic :: a Spring Framework demonstration</title>

Expand Down Expand Up @@ -85,11 +85,11 @@
<div class="col-12" style="margin-top: 10px; display: flex; justify-content: center;">
<a href="https://projects.spring.io/spring-boot/" target="_blank">
<img th:src="@{/resources/images/spring-boot-logo.svg}" alt="Powered by Spring Boot"
style="max-height: 42px; width: 42px; margin-left: 20px; margin-right: 20px;"></a>
style="max-height: 42px; width: 42px; margin-left: 20px; margin-right: 20px;" /></a>
<a href="https://kotlinlang.org/" target="_blank">
<img th:src="@{/resources/images/kotlin-logo.svg}"
alt="Powered by Kotlin"
style="max-height: 42px; width: 42px; margin-left: 20px; margin-right: 20px;"></a>
style="max-height: 42px; width: 42px; margin-left: 20px; margin-right: 20px;" /></a>
</div>
<div class="col-12 text-center" style="margin-top: 10px;">
Source: <a
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/templates/owners/ownerDetails.html
Expand Up @@ -16,15 +16,15 @@ <h2>Owner Information</h2>
</tr>
<tr>
<th>Address</th>
<td th:text="*{address}" /></td>
<td th:text="*{address}"/></td>
</tr>
<tr>
<th>City</th>
<td th:text="*{city}" /></td>
<td th:text="*{city}"/></td>
</tr>
<tr>
<th>Telephone</th>
<td th:text="*{telephone}" /></td>
<td th:text="*{telephone}"/></td>
</tr>
</table>

Expand Down Expand Up @@ -80,4 +80,4 @@ <h2>Pets and Visits</h2>

</body>

</html>
</html>
1 change: 1 addition & 0 deletions src/main/resources/templates/welcome.html
Expand Up @@ -17,6 +17,7 @@ <h3>Built with:</h3>
<li><a href="https://projects.spring.io/spring-boot/">Spring Boot</a></li>
<li><a href="https://kotlinlang.org/">The Kotlin programming language</a></li>
<li><a href="http://www.thymeleaf.org/">The Thymeleaf template engine</a></li>
<li><a href="https://docs.spring.io/spring-framework/docs/5.0.1.RELEASE/spring-framework-reference/web-reactive.html#spring-webflux">Spring WebFlux</a></li>
</ul>
</div>
</div>
Expand Down

0 comments on commit 279b2e7

Please sign in to comment.