Skip to content

Commit 47ac8c5

Browse files
committed
Add JsonUnwrapped
1 parent 0ce012f commit 47ac8c5

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dev.subhransu.fasterxmljacksonexamples.JsonUnwrapped;
2+
3+
import com.fasterxml.jackson.annotation.JsonUnwrapped;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
record JsonUnwrappedExample(String name, @JsonUnwrapped Address address) {}
8+
9+
record Address(String street, String city) {}
10+
11+
@RestController
12+
class JsonUnwrappedExampleController {
13+
@GetMapping("/json-unwrapped")
14+
public JsonUnwrappedExample getJsonUnwrapped() {
15+
return new JsonUnwrappedExample("A good name", new Address("A nice street", "A peaceful city"));
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.subhransu.fasterxmljacksonexamples.JsonUnwrapped;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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 org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
13+
@SpringBootTest
14+
@AutoConfigureMockMvc
15+
class JsonUnwrappedExampleControllerTest {
16+
17+
@Autowired private MockMvc mockMvc;
18+
19+
@Test
20+
void getJsonUnwrapped() throws Exception {
21+
mockMvc
22+
.perform(get("/json-unwrapped"))
23+
.andExpect(status().isOk())
24+
.andExpect(jsonPath("$.name").value("A good name"))
25+
.andExpect(jsonPath("$.street").value("A nice street"))
26+
.andExpect(jsonPath("$.city").value("A peaceful city"));
27+
}
28+
}

0 commit comments

Comments
 (0)