If entity classes are persisted through Spring Repositories, the solution is to inject PersistentEntityToJsonSchemaConverter
into the controller and invoke convert()
method.
Below is a code sample using Mongo as Repository
Entity class
@Document
public class Project implements Identifiable<String> {
@Id
private String id;
@NotNull
private String name;
//getters setters
}
Repository
public interface ProjectRepository extends
PagingAndSortingRepository<Project, String> {
}
Controller
@Controller
@ExposesResourceFor(Project.class)
@RequestMapping("/projects")
public class ProjectController {
@Autowired
private PersistentEntityToJsonSchemaConverter jsonSchemaConverter;
@RequestMapping(value = "/schema", method = RequestMethod.GET, produces = { "application/schema+json" })
@ResponseBody
public JsonSchema schema() {
return jsonSchemaConverter.convert(Project.class);
}
// implement rest of SDR mechanics
}