Skip to content

Commit 0ce012f

Browse files
committed
Add JsonAnyGetter and JsonAnySetter
1 parent 638c08e commit 0ce012f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.subhransu.fasterxmljacksonexamples.JsonAnyGetterSetter;
2+
3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4+
import com.fasterxml.jackson.annotation.JsonAnySetter;
5+
import java.util.Map;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
class JsonAnyGetterSetterController {
12+
@PostMapping("/json-any-getter-setter")
13+
public JsonAnyGetterSetterExample anyGetterSetter(@RequestBody JsonAnyGetterSetterExample data) {
14+
return data;
15+
}
16+
}
17+
18+
record JsonAnyGetterSetterExample(String name, Map<String, Object> properties) {
19+
@JsonAnyGetter
20+
public Map<String, Object> getProperties() {
21+
return properties;
22+
}
23+
24+
@JsonAnySetter
25+
public void setProperty(String name, Object value) {
26+
properties.put(name, value);
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package dev.subhransu.fasterxmljacksonexamples.JsonAnyGetterSetter;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
16+
@SpringBootTest
17+
@AutoConfigureMockMvc
18+
class JsonAnyGetterSetterExampleTest {
19+
20+
@Autowired private MockMvc mockMvc;
21+
@Autowired private ObjectMapper objectMapper;
22+
23+
@Test
24+
void testAnyGetterSetter() throws Exception {
25+
JsonAnyGetterSetterExample input =
26+
new JsonAnyGetterSetterExample(
27+
"John Doe",
28+
objectMapper.readValue(
29+
"""
30+
{
31+
"age": 30,
32+
"city": "New York",
33+
"address": {
34+
"street": "123 Main St",
35+
"zip": "10001"
36+
}
37+
}
38+
""",
39+
objectMapper
40+
.getTypeFactory()
41+
.constructMapType(Map.class, String.class, Object.class)));
42+
43+
mockMvc
44+
.perform(
45+
post("/json-any-getter-setter")
46+
.contentType(MediaType.APPLICATION_JSON)
47+
.content(objectMapper.writeValueAsString(input)))
48+
.andExpect(status().isOk())
49+
.andExpect(jsonPath("$.name").value("John Doe"))
50+
.andExpect(jsonPath("$.age").value(30))
51+
.andExpect(jsonPath("$.city").value("New York"))
52+
.andExpect(jsonPath("$.address.street").value("123 Main St"))
53+
.andExpect(jsonPath("$.address.zip").value("10001"));
54+
}
55+
}

0 commit comments

Comments
 (0)