Skip to content

Commit 971ffa2

Browse files
tomekl007retroq
authored andcommitted
Bael 738 (eugenp#1478)
* BAEL-724 code for put/patch article * BAEL-724 fix typo * BAEL-728 more generic patch approach
1 parent 7b8b5bf commit 971ffa2

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package org.baeldung.repository;
22

33
import org.baeldung.web.dto.HeavyResource;
4-
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
4+
import org.baeldung.web.dto.HeavyResourceAddressOnly;
5+
6+
import java.util.Map;
57

68
public class HeavyResourceRepository {
79

810
public void save(HeavyResource heavyResource) {
911
}
1012

11-
public void save(HeavyResourceAddressPartialUpdate partialUpdate) {
13+
public void save(HeavyResourceAddressOnly partialUpdate) {
14+
15+
}
16+
17+
public void save(Map<String, Object> updates, String id) {
1218

1319
}
1420
}

spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
import org.baeldung.repository.HeavyResourceRepository;
55
import org.baeldung.web.dto.HeavyResource;
6-
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
6+
import org.baeldung.web.dto.HeavyResourceAddressOnly;
77
import org.springframework.http.MediaType;
88
import org.springframework.http.ResponseEntity;
9-
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.RequestMapping;
11-
import org.springframework.web.bind.annotation.RequestMethod;
12-
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.Map;
1312

1413
@RestController
1514
public class HeavyResourceController {
@@ -23,9 +22,16 @@ public ResponseEntity<?> saveResource(@RequestBody HeavyResource heavyResource)
2322
}
2423

2524
@RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
26-
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) {
25+
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressOnly partialUpdate) {
2726
heavyResourceRepository.save(partialUpdate);
2827
return ResponseEntity.ok("resource address updated");
2928
}
3029

30+
@RequestMapping(value = "/heavy/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
31+
public ResponseEntity<?> partialUpdateGeneric(@RequestBody Map<String, Object> updates,
32+
@PathVariable("id") String id) {
33+
heavyResourceRepository.save(updates, id);
34+
return ResponseEntity.ok("resource updated");
35+
}
36+
3137
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.web.dto;
2+
3+
4+
public class HeavyResourceAddressOnly {
5+
private Integer id;
6+
private String address;
7+
8+
public HeavyResourceAddressOnly() {
9+
}
10+
11+
public HeavyResourceAddressOnly(Integer id, String address) {
12+
this.id = id;
13+
this.address = address;
14+
}
15+
16+
public Integer getId() {
17+
return id;
18+
}
19+
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
24+
public String getAddress() {
25+
return address;
26+
}
27+
28+
public void setAddress(String address) {
29+
this.address = address;
30+
}
31+
}

spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.baeldung.config.WebConfig;
55
import org.baeldung.web.dto.HeavyResource;
6-
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
6+
import org.baeldung.web.dto.HeavyResourceAddressOnly;
77
import org.junit.Before;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
@@ -16,6 +16,8 @@
1616
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1717
import org.springframework.web.context.WebApplicationContext;
1818

19+
import java.util.HashMap;
20+
1921
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
2022
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
2123
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -47,10 +49,21 @@ public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Ex
4749
}
4850

4951
@Test
50-
public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception {
52+
public void givenNewAddressOfResource_whenExecutePatchRequest_thenUpdateResourcePartially() throws Exception {
5153
mockMvc.perform(patch("/heavy")
5254
.contentType(MediaType.APPLICATION_JSON_VALUE)
53-
.content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue")))
55+
.content(objectMapper.writeValueAsString(new HeavyResourceAddressOnly(1, "5th avenue")))
56+
).andExpect(status().isOk());
57+
}
58+
59+
@Test
60+
public void givenNewAddressOfResource_whenExecutePatchGeneric_thenUpdateResourcePartially() throws Exception {
61+
HashMap<String, Object> updates = new HashMap<>();
62+
updates.put("address", "5th avenue");
63+
64+
mockMvc.perform(patch("/heavy/1")
65+
.contentType(MediaType.APPLICATION_JSON_VALUE)
66+
.content(objectMapper.writeValueAsString(updates))
5467
).andExpect(status().isOk());
5568
}
5669

0 commit comments

Comments
 (0)