From 4be8ba2c4349616e1da9d8232250e20da2daec47 Mon Sep 17 00:00:00 2001 From: Antoine Rey <antoine.rey@gmail.com> Date: Wed, 23 Aug 2017 18:38:39 +0200 Subject: [PATCH] Replace @RequestMapping by their corresponding shortcut @GetMapping and @PostMapping --- .../petclinic/owner/OwnerController.java | 27 +++++++++---------- .../petclinic/owner/PetController.java | 21 ++++++--------- .../petclinic/owner/VisitController.java | 17 +++++------- .../petclinic/system/CrashController.java | 3 ++- .../petclinic/system/WelcomeController.java | 4 +-- .../samples/petclinic/vet/VetController.java | 10 +++---- 6 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index ef3169b..d914ed7 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -15,22 +15,21 @@ */ package org.springframework.samples.petclinic.owner; -import java.util.Collection; -import java.util.Map; - -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; +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.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; +import javax.validation.Valid; +import java.util.Collection; +import java.util.Map; + /** * @author Juergen Hoeller * @author Ken Krebs @@ -54,14 +53,14 @@ class OwnerController { dataBinder.setDisallowedFields("id"); } - @RequestMapping(value = "/owners/new", method = RequestMethod.GET) + @GetMapping("/owners/new") public String initCreationForm(Map<String, Object> model) { Owner owner = new Owner(); model.put("owner", owner); return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } - @RequestMapping(value = "/owners/new", method = RequestMethod.POST) + @PostMapping("/owners/new") public String processCreationForm(@Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; @@ -71,13 +70,13 @@ class OwnerController { } } - @RequestMapping(value = "/owners/find", method = RequestMethod.GET) + @GetMapping("/owners/find") public String initFindForm(Map<String, Object> model) { model.put("owner", new Owner()); return "owners/findOwners"; } - @RequestMapping(value = "/owners", method = RequestMethod.GET) + @GetMapping("/owners") public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) { // allow parameterless GET request for /owners to return all records @@ -102,14 +101,14 @@ class OwnerController { } } - @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET) + @GetMapping("/owners/{ownerId}/edit") public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { Owner owner = this.owners.findById(ownerId); model.addAttribute(owner); return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; } - @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST) + @PostMapping("/owners/{ownerId}/edit") public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) { if (result.hasErrors()) { return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; @@ -126,7 +125,7 @@ class OwnerController { * @param ownerId the ID of the owner to display * @return a ModelMap with the model attributes for the view */ - @RequestMapping("/owners/{ownerId}") + @GetMapping("/owners/{ownerId}") public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { ModelAndView mav = new ModelAndView("owners/ownerDetails"); mav.addObject(this.owners.findById(ownerId)); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 45b87c6..9c52e03 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -15,21 +15,16 @@ */ package org.springframework.samples.petclinic.owner; -import java.util.Collection; - -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Collection; /** * @author Juergen Hoeller @@ -70,7 +65,7 @@ class PetController { dataBinder.setValidator(new PetValidator()); } - @RequestMapping(value = "/pets/new", method = RequestMethod.GET) + @GetMapping("/pets/new") public String initCreationForm(Owner owner, ModelMap model) { Pet pet = new Pet(); owner.addPet(pet); @@ -78,7 +73,7 @@ class PetController { return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } - @RequestMapping(value = "/pets/new", method = RequestMethod.POST) + @PostMapping("/pets/new") public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) { if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null){ result.rejectValue("name", "duplicate", "already exists"); @@ -93,14 +88,14 @@ class PetController { } } - @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET) + @GetMapping("/pets/{petId}/edit") public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { Pet pet = this.pets.findById(petId); model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } - @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST) + @PostMapping("/pets/{petId}/edit") public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) { if (result.hasErrors()) { pet.setOwner(owner); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java index d98c5dd..d7afed1 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java @@ -15,21 +15,16 @@ */ package org.springframework.samples.petclinic.owner; -import java.util.Map; - -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.visit.Visit; import org.springframework.samples.petclinic.visit.VisitRepository; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Map; /** * @author Juergen Hoeller @@ -76,13 +71,13 @@ class VisitController { } // Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called - @RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET) + @GetMapping("/owners/*/pets/{petId}/visits/new") public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) { return "pets/createOrUpdateVisitForm"; } // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called - @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) + @PostMapping("/owners/{ownerId}/pets/{petId}/visits/new") public String processNewVisitForm(@Valid Visit visit, BindingResult result) { if (result.hasErrors()) { return "pets/createOrUpdateVisitForm"; diff --git a/src/main/java/org/springframework/samples/petclinic/system/CrashController.java b/src/main/java/org/springframework/samples/petclinic/system/CrashController.java index a702cfc..c18c04d 100644 --- a/src/main/java/org/springframework/samples/petclinic/system/CrashController.java +++ b/src/main/java/org/springframework/samples/petclinic/system/CrashController.java @@ -16,6 +16,7 @@ package org.springframework.samples.petclinic.system; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -29,7 +30,7 @@ import org.springframework.web.bind.annotation.RequestMethod; @Controller class CrashController { - @RequestMapping(value = "/oups", method = RequestMethod.GET) + @GetMapping("/oups") public String triggerException() { throw new RuntimeException( "Expected: controller used to showcase what " + "happens when an exception is thrown"); diff --git a/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java b/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java index b5af0f7..00430a7 100644 --- a/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java +++ b/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java @@ -2,12 +2,12 @@ package org.springframework.samples.petclinic.system; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; @Controller class WelcomeController { - @RequestMapping("/") + @GetMapping("/") public String welcome() { return "welcome"; } diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java index 8ddcca6..7ce8374 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java @@ -15,13 +15,13 @@ */ package org.springframework.samples.petclinic.vet; -import java.util.Map; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; +import java.util.Map; + /** * @author Juergen Hoeller * @author Mark Fisher @@ -38,7 +38,7 @@ class VetController { this.vets = clinicService; } - @RequestMapping(value = { "/vets.html" }) + @GetMapping("/vets.html") public String showVetList(Map<String, Object> model) { // Here we are returning an object of type 'Vets' rather than a collection of Vet // objects so it is simpler for Object-Xml mapping @@ -48,7 +48,7 @@ class VetController { return "vets/vetList"; } - @RequestMapping(value = { "/vets.json", "/vets.xml" }) + @GetMapping({ "/vets.json", "/vets.xml" }) public @ResponseBody Vets showResourcesVetList() { // Here we are returning an object of type 'Vets' rather than a collection of Vet // objects so it is simpler for JSon/Object mapping -- GitLab