@RepositoryRestResource 每次重新启动应用程序时都会更改 url

问题描述

我有一个扩展 JpaRepositoryNameRepositoryCustom 的存储库接口。 我的存储库用 @RepositoryRestRessource(collectionResourceRel="pathname",path="pathname") 注释。 我遇到的问题是,我的应用程序每重新启动一次,存储库的 URL 就会更改,因此我无法在我定义的 URL 下找到存储库的公开数据,并且某些功能(例如存储库搜索)未公开也不再使用 API。

“NameRepositroyCustom”用于搜索功能,该功能使用另一个存储库为前端的搜索栏实现 JPA Criteria Api 规范。

有没有人有解决方案?唯一标注为 @RepositoryRestRessource 的存储库是实现所有其他存储库的主存储库。 NameRepositorySpec 带有 @Repository 注释,这可能是原因吗?

编辑:我以代码为例来阐明上述类和接口之间的关系。 这是与持久化在数据库中的实体相关的基本存储库:

    @RepositoryRestResource(collectionRessourceRel = "enitynames",path = "entitynames")
public interface EntitynameRepository extends JpaRepository<Entityname,Long>,EntitynameRepositoryCustom{

    //custom methods in here
}

这是自定义存储库:

 public interface EntitynameRepositoryCustom {

    Page<Entityname> search(String exampleParam1,String exampleParam2,Pageable pageable);
}

这是自定义存储库的实现:

public class EntitynameRepositoryCustomImpl implements EntitynameRepositoryCustom{

    @Autowired
    EntityManager em;

    @Autowired
    EntitynameRepositorySpec entitynameRepositorySpec;

    Specification<Entityname> querySpecification = null;

    @Override
    public Page<Entityname> search(String exampleParam1,Pageable pageable) {
        //Code here uses the criteria builder and Specification to generate a custom query with optional parameters
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery<Entityname> cq = cb.createquery(Entityname.class);
        //Code below is done for every passed in parameter
        if(exampleParam1 != null){
            Specification<Entityname> param1Specification = EntitynameSpecification.likeParam1(exampleParam1);
            querySpecification = Specification.where(param1Specification);
        } else {
            return null;
        }

        return entitynameRepositorySpec.findAll(specification,pageable);
    }
}

这是规范存储库:

public interface EntitynameRepositorySpec extends JpaRepository<Entityname,JpaSpecificationExecutor<Entityname>{
}

这是规范的实现:

public class EntitynameSpecification {

    public static Specification<Entityname> likeExampleParam1(String exampleParam1){
        if(exampleParam1 == null){
            return null;
        }
        return(root,query,cb) -> {
            reutrn cb.like(root.get("fieldname"),"%"+ exampleParam1 + "%");
        };
    }
}

与我的示例相比,存储库的 URL 更改为实体名称的一部分,类似于: entityname has URL: /entityname 如果发生错误,URL 将更改为 /name。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)