代码之家  ›  专栏  ›  技术社区  ›  OTUser

Spring数据REST 2.6.10:通过REST API通过嵌入的属性值查找

  •  0
  • OTUser  · 技术社区  · 7 年前

    我有 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)
    }
    

    如果我试图找到所有活跃的 规则配置 通过下面的休息API,它工作得很好

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

    但是当我试图找到所有活跃的 规则配置 针对特定 ruleset 通过下面的休息API它抛出 404 错误

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

    有人能帮我找到嵌入属性的具体值吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Manuel Ortiz    7 年前

    使用Spring数据REST,您不能开箱即用。

    使用自定义处理程序进行尝试。下面我留下一个能起作用的例子

    @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); 
        }
    
    }
    

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