Add jcenter as a repository, for example:
repositories {
jcenter()
}
Add the crnk-bom to your dependency management, for example:
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE'
mavenBom "io.crnk:crnk-bom:${CRNK_VERSION}"
}
}
And add the following dependencies to your classpath:
compile 'io.crnk:crnk-setup-spring-boot2'
compile 'io.crnk:crnk-format-plain-json'
compile 'io.crnk:crnk-home'
compile 'org.springframework.boot:spring-boot-starter-web'
Setup a Spring Boot application:
@Configuration
@SpringBootApplication
public class MinimalSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MinimalSpringBootApplication.class, args);
System.out.println("visit http://127.0.0.1:8080/ in your browser");
}
}
The actual data objects known as resource in JSON:API terminology looks like:
@JsonApiResource(type = "projects")
public class Project {
@JsonApiId
private Long id;
@JsonProperty
private String name;
public Project() {
}
public Project(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The actual implementation of a JSON:API repository. This example stores all data in-memory.
Notice
the QuerySpec.apply to do sorting, filtering and patching in-memory. More advanced use cases
will translate the QuerySpec to a native query of the underlying data store. The
@ApplicationScoped will lead for the repository to get picked up by the crnk-cdi integration.
@Component
public class ProjectRepositoryImpl extends ResourceRepositoryBase<Project, Long> {
private static final AtomicLong ID_GENERATOR = new AtomicLong(124);
private Map<Long, Project> projects = new HashMap<>();
public ProjectRepositoryImpl() {
super(Project.class);
}
@Override
public synchronized void delete(Long id) {
projects.remove(id);
}
@Override
public synchronized <S extends Project> S save(S project) {
if (project.getId() == null) {
project.setId(ID_GENERATOR.getAndIncrement());
}
projects.put(project.getId(), project);
return project;
}
@Override
public synchronized ResourceList<Project> findAll(QuerySpec querySpec) {
return querySpec.apply(projects.values());
}
}