An open API service indexing awesome lists of open source software.

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

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