From 65ef69372800a31c169b1b93849c87ba2ab2310b Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Mar 2017 11:19:35 +0100 Subject: [PATCH 1/3] BAEL-724 code for put/patch article --- .../repository/HeavyResourceRepository.java | 14 +++++ .../controller/HeavyResourceController.java | 31 ++++++++++ .../org/baeldung/web/dto/HeavyResource.java | 62 +++++++++++++++++++ .../HeavyResourceAddressPartialUpdate.java | 31 ++++++++++ .../HeavyResourceControllerTest.java | 57 +++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java create mode 100644 spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java diff --git a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java new file mode 100644 index 000000000000..cff78442d03f --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java @@ -0,0 +1,14 @@ +package org.baeldung.repository; + +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; + +public class HeavyResourceRepository { + + public void save(HeavyResource heavyResource) { + } + + public void save(HeavyResourceAddressPartialUpdate partialUpdate) { + + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java new file mode 100644 index 000000000000..a2d5cfbd7b29 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java @@ -0,0 +1,31 @@ +package org.baeldung.web.controller; + + +import org.baeldung.repository.HeavyResourceRepository; +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HeavyResourceController { + + private HeavyResourceRepository heavyResourceRepository = new HeavyResourceRepository(); + + @RequestMapping(value = "/heavy", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity saveResource(@RequestBody HeavyResource heavyResource) { + heavyResourceRepository.save(heavyResource); + return ResponseEntity.ok("resource saved"); + } + + @RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) { + heavyResourceRepository.save(partialUpdate); + return ResponseEntity.ok("resource address updated"); + } + +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java new file mode 100644 index 000000000000..934e76606f01 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java @@ -0,0 +1,62 @@ +package org.baeldung.web.dto; + + +public class HeavyResource { + private Integer id; + private String name; + private String surname; + private Integer age; + private String address; + + + public HeavyResource() { + } + + public HeavyResource(Integer id, String name, String surname, Integer age, String address) { + this.id = id; + this.name = name; + this.surname = surname; + this.age = age; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java new file mode 100644 index 000000000000..f90f02ea08fe --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java @@ -0,0 +1,31 @@ +package org.baeldung.web.dto; + + +public class HeavyResourceAddressPartialUpdate { + private Integer id; + private String address; + + public HeavyResourceAddressPartialUpdate() { + } + + public HeavyResourceAddressPartialUpdate(Integer id, String address) { + this.id = id; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java new file mode 100644 index 000000000000..beb32a819621 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java @@ -0,0 +1,57 @@ +package org.baeldung.web.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.baeldung.config.WebConfig; +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class HeavyResourceControllerTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception { + mockMvc.perform(put("/heavy") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street"))) + ).andExpect(status().isOk()); + } + + @Test + public void givenNewAddressOfResouce_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception { + mockMvc.perform(patch("/heavy") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue"))) + ).andExpect(status().isOk()); + } + +} \ No newline at end of file From df6de716c0333ec01949c740f406ba0fb4b98817 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Mar 2017 11:21:44 +0100 Subject: [PATCH 2/3] BAEL-724 fix typo --- .../baeldung/web/controller/HeavyResourceControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java index beb32a819621..e68506701d69 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java @@ -47,7 +47,7 @@ public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Ex } @Test - public void givenNewAddressOfResouce_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception { + public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception { mockMvc.perform(patch("/heavy") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue"))) From 684661a8a0d6a64e2a6952685cc7260c70ecba2c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 23 Mar 2017 15:30:12 +0100 Subject: [PATCH 3/3] BAEL-728 more generic patch approach --- .../repository/HeavyResourceRepository.java | 10 ++++++++-- .../controller/HeavyResourceController.java | 18 ++++++++++++------ ...ate.java => HeavyResourceAddressOnly.java} | 6 +++--- .../HeavyResourceControllerTest.java | 19 ++++++++++++++++--- 4 files changed, 39 insertions(+), 14 deletions(-) rename spring-rest/src/main/java/org/baeldung/web/dto/{HeavyResourceAddressPartialUpdate.java => HeavyResourceAddressOnly.java} (70%) diff --git a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java index cff78442d03f..6de9e7545035 100644 --- a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java +++ b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java @@ -1,14 +1,20 @@ package org.baeldung.repository; import org.baeldung.web.dto.HeavyResource; -import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.baeldung.web.dto.HeavyResourceAddressOnly; + +import java.util.Map; public class HeavyResourceRepository { public void save(HeavyResource heavyResource) { } - public void save(HeavyResourceAddressPartialUpdate partialUpdate) { + public void save(HeavyResourceAddressOnly partialUpdate) { + + } + + public void save(Map updates, String id) { } } diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java index a2d5cfbd7b29..f2c4ffaa5112 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java @@ -3,13 +3,12 @@ import org.baeldung.repository.HeavyResourceRepository; import org.baeldung.web.dto.HeavyResource; -import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.baeldung.web.dto.HeavyResourceAddressOnly; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; @RestController public class HeavyResourceController { @@ -23,9 +22,16 @@ public ResponseEntity saveResource(@RequestBody HeavyResource heavyResource) } @RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) { + public ResponseEntity partialUpdateName(@RequestBody HeavyResourceAddressOnly partialUpdate) { heavyResourceRepository.save(partialUpdate); return ResponseEntity.ok("resource address updated"); } + @RequestMapping(value = "/heavy/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity partialUpdateGeneric(@RequestBody Map updates, + @PathVariable("id") String id) { + heavyResourceRepository.save(updates, id); + return ResponseEntity.ok("resource updated"); + } + } diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressOnly.java similarity index 70% rename from spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java rename to spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressOnly.java index f90f02ea08fe..f96347d60cf4 100644 --- a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java +++ b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressOnly.java @@ -1,14 +1,14 @@ package org.baeldung.web.dto; -public class HeavyResourceAddressPartialUpdate { +public class HeavyResourceAddressOnly { private Integer id; private String address; - public HeavyResourceAddressPartialUpdate() { + public HeavyResourceAddressOnly() { } - public HeavyResourceAddressPartialUpdate(Integer id, String address) { + public HeavyResourceAddressOnly(Integer id, String address) { this.id = id; this.address = address; } diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java index e68506701d69..e283c5f5f6e1 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.baeldung.config.WebConfig; import org.baeldung.web.dto.HeavyResource; -import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.baeldung.web.dto.HeavyResourceAddressOnly; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -16,6 +16,8 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import java.util.HashMap; + import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -47,10 +49,21 @@ public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Ex } @Test - public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception { + public void givenNewAddressOfResource_whenExecutePatchRequest_thenUpdateResourcePartially() throws Exception { mockMvc.perform(patch("/heavy") .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue"))) + .content(objectMapper.writeValueAsString(new HeavyResourceAddressOnly(1, "5th avenue"))) + ).andExpect(status().isOk()); + } + + @Test + public void givenNewAddressOfResource_whenExecutePatchGeneric_thenUpdateResourcePartially() throws Exception { + HashMap updates = new HashMap<>(); + updates.put("address", "5th avenue"); + + mockMvc.perform(patch("/heavy/1") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(updates)) ).andExpect(status().isOk()); }