https://github.com/jusexton/spring-expandable-fields-example
Example implementation of expandable objects with spring
https://github.com/jusexton/spring-expandable-fields-example
example spring-boot
Last synced: 7 months ago
JSON representation
Example implementation of expandable objects with spring
- Host: GitHub
- URL: https://github.com/jusexton/spring-expandable-fields-example
- Owner: jusexton
- Created: 2020-01-13T04:59:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-03T19:51:22.000Z (almost 6 years ago)
- Last Synced: 2025-04-13T13:09:06.701Z (10 months ago)
- Topics: example, spring-boot
- Language: Java
- Homepage:
- Size: 64.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Expandable Fields Example
Sometimes it will be beneficial to not allow your API consumers to "drink from the fire hose of data". Expandable entity fields can help.
## In Action
### The Models
Some example domain models we will use for demonstration purposes
#### Person
```java
public class Person {
private String firstName;
private String lastName;
@JsonExpandableField
private ContactInformation contactInformation;
}
```
#### Contact Information
```java
public class ContactInformation {
@JsonExpandableField
private Address address;
@JsonExpandableField
private Phone phone;
}
```
#### Address
```java
public class Address {
private String street;
private String city;
private String state;
private String zip;
}
```
#### Phone
```java
public class Phone {
private String phoneNumber;
private PhoneType phoneType;
}
```
#### Phone Type
```java
public enum PhoneType {
HOME, CELL, WORK
}
```
### The Requests
#### localhost:8080/people
```json
{
"firstName": "Justin",
"lastName": "Sexton",
"contactInformation": {}
}
```
#### localhost:8080/people?expand=contactinformation
```json
{
"firstName": "Justin",
"lastName": "Sexton",
"contactInformation": {
"address": {},
"phone": {}
}
}
```
#### localhost:8080/people?expand=contactinformation.address
```json
{
"firstName": "Justin",
"lastName": "Sexton",
"contactInformation": {
"address": {
"street": "1234 Street Drive",
"city": "City",
"state": "State",
"zip": "12345"
},
"phone": {}
}
}
```
#### localhost:8080/people?expand=contactinformation.address,contactinformation.phone
```json
{
"firstName": "Justin",
"lastName": "Sexton",
"contactInformation": {
"address": {
"street": "1234 Street Drive",
"city": "City",
"state": "State",
"zip": "12345"
},
"phone": {
"phoneNumber": "1234567890",
"phoneType": "CELL"
}
}
}
```
## Troubles Serializing Map
Currenty when serializing a map, there is no way to distinguish map entries as expandable or not, so all entries will still be serialized. It is advised to convert the map to an object that makes use of the custom expandable annotations.
Heres a useful link for converting maps to a POJO object. https://stackoverflow.com/questions/25447611/how-to-deserialize-a-mapstring-object-into-pojo