Spring Framework 4.3 has some Web improvements. In the article, JavaSampleApproach will introduce you about New Feature RequestMapping: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping.
Related Posts:
– Spring MVC – @RequestMapping with Methods, Headers, Params, @PathVariable and @RequestParam
– Kotlin Spring MVC RequestMapping RESTful APIs with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping | SpringBoot Example
Contents
I. Technologies
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.3.RELEASE
II. Overview
1. Project Structure
2. Step to do
– Create SpringBoot project
– Create a simple model
– Create New Feature RequestMapping
– Run and check result
III. Practices
1. Create SpringBoot project
Open SpringToolSuite, on main menu, choose File->New->Spring Boot Starter, input project information. Press Next, then Finish, a SpringBoot project is created succesfully.
Open pom.xml file, add Web dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
2. Create a simple model
package com.javasampleapproach.newfeaturerequestmapping.model; public class Customer { private int custId; private String firstname; private String lastname; private int age; public Customer(){} public Customer(int custId, String firstname, String lastname, int age){ this.custId = custId; this.firstname = firstname; this.lastname = lastname; this.age = age; } public Customer(String firstname, String lastname, int age){ this.firstname = firstname; this.lastname = lastname; this.age = age; } public int getCustId() { return custId; } public void setCustId(int custId) { this.custId = custId; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { String info = String.format("custId = %d, firstname = %s, lastname = %s, age = %d", custId, firstname, lastname, age); return info; } } |
3. Create New Feature RequestMapping
Create @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
package com.javasampleapproach.newfeaturerequestmapping.controller; import java.util.HashMap; import java.util.Map; import java.util.Random; import javax.annotation.PostConstruct; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.javasampleapproach.newfeaturerequestmapping.model.Customer; @RestController public class WebController { Map<Integer, Customer> custStores = new HashMap<Integer, Customer>(); @PostConstruct public void initIt() throws Exception { Customer cust1 = new Customer(1, "Jack", "Smith", 20); Customer cust2 = new Customer(2, "Peter", "Johnson", 25); custStores.put(cust1.getCustId(), cust1); custStores.put(cust2.getCustId(), cust2); } @GetMapping("/get") public Customer getMethod(@RequestParam("custId") int custId) { return custStores.get(custId); } @PostMapping("/post") public Customer postMethod(@RequestBody Customer customer) { Random r = new Random(); customer.setCustId(r.nextInt()); // POST processing custStores.put(customer.getCustId(), customer); // Log out custStores after POST System.out.println("Customer Stores after POST:"); custStores.forEach((id, cust) -> System.out.println(cust.toString())); return customer; } @PutMapping("/put/{custId}") public Customer putMethod(@PathVariable int custId, @RequestBody Customer customer) { // PUT processing try{ custStores.remove(custId); customer.setCustId(custId); custStores.put(custId, customer); }catch(Exception e){ System.out.println(e.getStackTrace()); return null; } // Log out custStores after PUT System.out.println("Customer Stores after PUT"); custStores.forEach((id, cust) -> System.out.println(cust.toString())); return customer; } @DeleteMapping("/delete/{custId}") public String deleteMethod(@PathVariable int custId) { try { // DELETE processing custStores.remove(custId); } catch (Exception e) { return "Error"; } // Log out custStores after DELETE System.out.println("Customer Stores after DELETE"); custStores.forEach((id, cust) -> System.out.println(cust.toString())); return "Done"; } } |
4. Run and check result
Build & Run SpringBoot project then check result:
– Get RequestMapping:
– POST RequestMapping:
– PUT RequestMapping:
– DELETE RequestMapping:
ResfulWebServices Logs:
Customer Stores after POST: custId = 1642586151, firstname = Mary, lastname = Taylor, age = 27 custId = 1, firstname = Jack, lastname = Smith, age = 20 custId = 2, firstname = Peter, lastname = Johnson, age = 25 Customer Stores after PUT custId = 1642586151, firstname = Amy, lastname = Taylor, age = 24 custId = 1, firstname = Jack, lastname = Smith, age = 20 custId = 2, firstname = Peter, lastname = Johnson, age = 25 Customer Stores after DELETE custId = 1, firstname = Jack, lastname = Smith, age = 20 custId = 2, firstname = Peter, lastname = Johnson, age = 25 |
IV. Sourcecode
SpringMVCNewFeatureRequestMapping
Last updated on October 31, 2017.
is package scanning configuration required to tell Spring which Java source packages to initialize the beans from?
Hi,
With SpringBoot application, we have:
@SpringBootApplication
=@Configuration
+@EnableAutoConfiguration
+@ComponentScan
.@EnableAutoConfiguration
annotation implicitly defines a base search package is the root package:com.javasampleapproach.newfeaturerequestmapping
.Regards,
A little confused here. In your code you defined:
@RequestParam("custId")
is my confusion because in your demo example you’re fetching the id param via /get?id=1Shouldn’t that be
/get?custId=1
???
Great tut overall.
Thanks!
Hi Emko,
We should change code as:
1. For request:
/get?id=1
2. Or use:
/get?custId=1
with the mapping:Thanks for your notice
Regards,
JSA
Can show the difference between implementing of @patchmapping and @putmapping?