Spring data rest 2.6.10:通过 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)
}

如果我试图ruleConfigurations通过下面的 rest api找到所有活动的,它工作正常

https://localhost/api/ruleConfigurations/search/findByActive?active=1

但是当我试图通过下面的rest api找到ruleConfigurations特定的所有活动时ruleset它会抛出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); 
    }

}

链接到文档https://docs.spring.io/spring-data/rest/docs/3.1.x/reference/html/#customizing-sdr.overriding-sdr-response-handlers

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章