Spring data rest 2.6.10:通过 Rest 按嵌入属性值查找 api
Spring data rest 2.6.10 : Find by embedded attributes value via Rest api
我有 Ruleset
个具有嵌入式属性 ruleConfigurations
的实体,我正在尝试通过 Rest 获取具有活动规则配置的规则集,下面是我的实体
@Entity
@Table(name = "ruleset")
class RuleSet implements Serializable {
private static final long serialVersionUID = 1L
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
@Nationalized
@Column(unique = true, nullable = false)
private String name
@Column(nullable = false)
private boolean active
@ManyToMany(cascade = CascadeType.ALL, targetEntity = Rule.class)
private Set<Rule> rules
@OneToMany(cascade=CascadeType.ALL, targetEntity = RuleConfiguration.class)
private Set<RuleConfiguration> ruleConfigurations;
//getters and setters
}
@Entity
@Table(name = "ruleconfiguration")
class RuleConfiguration implements Serializable {
private static final long serialVersionUID = 1L
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
@Nationalized
@Column(nullable = false)
private String name
@Nationalized
@Column(nullable = false)
@ColumnDefault("1")
private boolean active
@Nationalized
@Column(nullable = true)
private String value
//getters and setters
}
下面是我的RuleConfigurationRepository
@RepositoryRestResource(collectionResourceRel = "ruleConfigurations", path = "ruleConfigurations")
interface RuleConfigurationRepository extends CrudRepository<RuleConfiguration, Long> {
List<RuleConfiguration> findByActive(@Param("active")Boolean active)
}
如果我试图通过下面的 rest api 找到所有活跃的 ruleConfigurations
它工作正常
https://localhost/api/ruleConfigurations/search/findByActive?active=1
但是当我试图通过下面的 rest api 找到特定 ruleset
的所有活动 ruleConfigurations
时,它会抛出 404
错误
https://localhost/api/ruleSets/7/ruleConfigurations/search/findByActive?active=1
有人可以帮我找到嵌入属性的具体值吗?
你不能用 spring 数据休息来开箱即用。
尝试使用自定义处理程序。下面我留下一个可以工作的例子
@RepositoryRestController
public class RuleSetCustomController {
private final RuleSetRespositry repository;
@Autowired
public RuleSetCustomController(RuleSetRespositry repo) {
this.repository = repo;
}
@RequestMapping(method = RequestMethod.GET, value = "/ruleSets/{ruleSetId}/ruleConfigurations/search/findByActive")
public @ResponseBody ResponseEntity<?> getProducers(@PathVariable Long ruleSetId, @RequestParam Boolean active) {
RuleSet ruleSet = repository.findOne(ruleSetId);
if(ruleSet == null) return ResponseEntity.notFound().build();
List<RuleConfiguration> resp = ruleSet.getRuleConfigurations().stream().filter(ruleConf -> ruleConf.isActive() == active).collect(Collectors.toList());
Resources<RuleConfiguration> resources = new Resources<RuleConfiguration>(resp);
return ResponseEntity.ok(resources);
}
}
我有 Ruleset
个具有嵌入式属性 ruleConfigurations
的实体,我正在尝试通过 Rest 获取具有活动规则配置的规则集,下面是我的实体
@Entity
@Table(name = "ruleset")
class RuleSet implements Serializable {
private static final long serialVersionUID = 1L
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
@Nationalized
@Column(unique = true, nullable = false)
private String name
@Column(nullable = false)
private boolean active
@ManyToMany(cascade = CascadeType.ALL, targetEntity = Rule.class)
private Set<Rule> rules
@OneToMany(cascade=CascadeType.ALL, targetEntity = RuleConfiguration.class)
private Set<RuleConfiguration> ruleConfigurations;
//getters and setters
}
@Entity
@Table(name = "ruleconfiguration")
class RuleConfiguration implements Serializable {
private static final long serialVersionUID = 1L
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id
@Nationalized
@Column(nullable = false)
private String name
@Nationalized
@Column(nullable = false)
@ColumnDefault("1")
private boolean active
@Nationalized
@Column(nullable = true)
private String value
//getters and setters
}
下面是我的RuleConfigurationRepository
@RepositoryRestResource(collectionResourceRel = "ruleConfigurations", path = "ruleConfigurations")
interface RuleConfigurationRepository extends CrudRepository<RuleConfiguration, Long> {
List<RuleConfiguration> findByActive(@Param("active")Boolean active)
}
如果我试图通过下面的 rest api 找到所有活跃的 ruleConfigurations
它工作正常
https://localhost/api/ruleConfigurations/search/findByActive?active=1
但是当我试图通过下面的 rest api 找到特定 ruleset
的所有活动 ruleConfigurations
时,它会抛出 404
错误
https://localhost/api/ruleSets/7/ruleConfigurations/search/findByActive?active=1
有人可以帮我找到嵌入属性的具体值吗?
你不能用 spring 数据休息来开箱即用。
尝试使用自定义处理程序。下面我留下一个可以工作的例子
@RepositoryRestController
public class RuleSetCustomController {
private final RuleSetRespositry repository;
@Autowired
public RuleSetCustomController(RuleSetRespositry repo) {
this.repository = repo;
}
@RequestMapping(method = RequestMethod.GET, value = "/ruleSets/{ruleSetId}/ruleConfigurations/search/findByActive")
public @ResponseBody ResponseEntity<?> getProducers(@PathVariable Long ruleSetId, @RequestParam Boolean active) {
RuleSet ruleSet = repository.findOne(ruleSetId);
if(ruleSet == null) return ResponseEntity.notFound().build();
List<RuleConfiguration> resp = ruleSet.getRuleConfigurations().stream().filter(ruleConf -> ruleConf.isActive() == active).collect(Collectors.toList());
Resources<RuleConfiguration> resources = new Resources<RuleConfiguration>(resp);
return ResponseEntity.ok(resources);
}
}