Question

What is the best way to implement "/schema" function on custom controllers (explained in rest-bucks ) similar to "[repository]/schema" function with Spring Data Rest?

Was it helpful?

Solution

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top