* BAEL-724 code for put/patch article

* BAEL-724 fix typo

* BAEL-728 more generic patch approach

* BAEL-738 change url of PUT method

* fix route confict
This commit is contained in:
Tomasz Lelek 2017-03-26 15:56:20 +02:00 committed by Eugen
parent 052c149b0e
commit 52eb7c2bc4
3 changed files with 18 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package org.baeldung.repository;
import org.baeldung.web.dto.HeavyResource;
import org.baeldung.web.dto.HeavyResourceAddressOnly;
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
import java.util.Map;
@ -17,4 +18,11 @@ public class HeavyResourceRepository {
public void save(Map<String, Object> updates, String id) {
}
public void save(HeavyResource heavyResource, String id) {
}
public void save(HeavyResourceAddressOnly partialUpdate, String id) {
}
}

View File

@ -15,19 +15,19 @@ 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);
@RequestMapping(value = "/heavyresource/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> saveResource(@RequestBody HeavyResource heavyResource, @PathVariable("id") String id) {
heavyResourceRepository.save(heavyResource, id);
return ResponseEntity.ok("resource saved");
}
@RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressOnly partialUpdate) {
heavyResourceRepository.save(partialUpdate);
@RequestMapping(value = "/heavyresource/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressOnly partialUpdate, @PathVariable("id") String id) {
heavyResourceRepository.save(partialUpdate, id);
return ResponseEntity.ok("resource address updated");
}
@RequestMapping(value = "/heavy/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/heavyresource2/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> partialUpdateGeneric(@RequestBody Map<String, Object> updates,
@PathVariable("id") String id) {
heavyResourceRepository.save(updates, id);

View File

@ -42,7 +42,7 @@ public class HeavyResourceControllerTest {
@Test
public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception {
mockMvc.perform(put("/heavy")
mockMvc.perform(put("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street")))
).andExpect(status().isOk());
@ -50,7 +50,7 @@ public class HeavyResourceControllerTest {
@Test
public void givenNewAddressOfResource_whenExecutePatchRequest_thenUpdateResourcePartially() throws Exception {
mockMvc.perform(patch("/heavy")
mockMvc.perform(patch("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(new HeavyResourceAddressOnly(1, "5th avenue")))
).andExpect(status().isOk());
@ -61,7 +61,7 @@ public class HeavyResourceControllerTest {
HashMap<String, Object> updates = new HashMap<>();
updates.put("address", "5th avenue");
mockMvc.perform(patch("/heavy/1")
mockMvc.perform(patch("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(updates))
).andExpect(status().isOk());