문제

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?

도움이 되었습니까?

해결책

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

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top