在自定义控制器上实现“ / schema ”功能的最佳方法是什么(在rest-bucks中解释)类似于“ [repository] / schema ” Spring Data Rest功能?
答案 0 :(得分:2)
如果通过Spring Repositories持久保存实体类,解决方案是将PersistentEntityToJsonSchemaConverter
注入控制器并调用convert()
方法。
以下是使用Mongo作为存储库的代码示例
实体类
@Document
public class Project implements Identifiable<String> {
@Id
private String id;
@NotNull
private String name;
//getters setters
}
<强>存储库强>
public interface ProjectRepository extends
PagingAndSortingRepository<Project, String> {
}
<强>控制器强>
@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
}