Simple implementation of the JSON:API specification (only required output fields). This library implements only top-level fields: data, links, errors and meta without any relationships, includes and others.
Often we only need standard of output all our endpoints especially when using many types of communications like HTTP query, websockets, queues etc and don't need complex entities inner relationships in our API but it's good if implementing some exists standards so this library for this goals.
Add dependency to your project:
<dependency>
<groupId>com.slm-dev</groupId>
<artifactId>jsonapi-simple</artifactId>
<version>1.12.0</version>
</dependency>Warning: The id field is included in the attributes because it more convenient for API clients.
See documentation part: response structure
Each response DTO should contain the annotation @JsonApiType("resource-type") with any resource type identifier and
the annotation @JsonApiId without arguments on field which will be unique identifier this item, usually this
field is id.
If we want to use data types like lists, maps etc. in the response we can set manually data type to avoid exception (see example below).
Each response may contain generics (if you planning to use OPENAPI) or may not (without OPENAPI), for example both variants correct:
public class RestController {
// The simplest option without OPENAPI support
public Response responseWithoutGenerics() {
return Response.builder()
.build();
}
// This option will display correctly in OPENAPI with all DTO fields
public Response<SomeDto> responseWithGenerics() {
return Response.<SomeDto, SomeDto>builder()
.build();
}
}Parametrized response may be 2 types:
- as list - first parameter in response builder must be only
<List<Data<YourDto>>>:public class RestController { public Response<List<Data<SomeDto>>> responseAsList() { return Response.<List<Data<SomeDto>>, SomeDto>builder() .build(); } }
- as object:
public class RestController { public Response<SomeDto> responseAsObject() { return Response.<SomeDto, SomeDto>builder() .build(); } }
- as empty response with
Voidclass as parameter:public class RestController { public Response<Void> responseAsObject() { return Response.<Void, Void>builder() .build(); } }
Data types other than the DTO class (lists, maps etc.) can't have required annotations, so we can set data type manually:
@RestController
@RequestMapping(value = "/api/v1/app", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RestController {
@GetMapping
public Response<SomeDto> responseAsObject() {
return Response.<SomeDto, SomeDto>builder()
.jsonApiId("some-id")
.jsonApiType("some-data-type")
.data(Map.of("name", "Test string"))
.build();
}
}This produces response like:
{
"data": {
"type":"some-data-type",
"id":"<random-uuid-id> or <fixed-id>",
"attributes": {
"id":"<random-uuid-id> or <fixed-id>",
"name": "Test string"
},
"links": {
"self": "/some-data-type/<random-uuid-id>"
}
},
"meta": {
"api": {
"version":"1"
},
"page": {
"maxSize":25,
"total":1
}
}
}