我建立弹簧引导后端REST API。使用弹簧启动内部@Controllers提供搜索方法
实体:
@Entity
public class Club {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
@Column(unique=true)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
库:
@RepositoryRestResource
public interface ClubRepository extends JpaRepository<Club, Long>, JpaSpecificationExecutor<Club> {
}
仅这一点就暴露了一个休息终点在http://host/clubs
,太好了。现在,我想允许网址中的一些参数用于搜索目的,所以我开始遵循http://www.baeldung.com/rest-api-search-language-spring-data-specifications的说明。
但他们最终创造出一个自定义的@Controller
传递请求参数:
@Controller
public class ClubController {
@Autowired
private ClubRepository repo;
@RequestMapping(method = RequestMethod.GET, value = "/clubs")
@ResponseBody
public List<Club> search(@RequestParam(value = "search") String search) {
/* ... */
return repo.findAll(spec);
}
}
所以你看,他们最终调用的存储库的findAll方法,只是路过他们建立基于该规范查询参数。很简单,但我真的很想不必为每个域对象创建额外的控制器。换句话说,有提供这种搜索通过在存储库中直接注释(例如)的@Entity
,或覆盖方法(如findAll
方法)的特征的方法吗?
.... <3 you rock – Sebas
是的,我既不是工作!也许不赞成,应该是流程资源? –
没有,我找到了,'的apt-Maven的插件:1.1.3:process'这不是一个标准的目标。 – Sebas