收藏AngularJS前端(直接访问) - 春天的数据后台休息 [英] Bookmark AngularJS frontend (direct access) - Spring Data Rest backend

查看:904
本文介绍了收藏AngularJS前端(直接访问) - 春天的数据后台休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始实施网络前端比传统的关系数据库来处理由担任RESTful服务数据春季休息。这与 AngularJS 角-HAL 图书馆,我发现非常适合 HATEOAS 哲学和形式主义>弹簧数据休息。

I've started to implement a web-front end to manipulate a RESTful service served by Spring Data Rest over a traditional relational DB. This is done with AngularJS and the angular-hal library that I found fits very well the HATEOAS philosophy and formalism of Spring Data Rest.

例如,我只需要知道的第一API端点('/'),然后我所有的查询都是通过关系,而不关心的URL完成。
我的问题是要能够直接访问显示我的实体之一的页

For example, I just need to know the first API endpoint ('/'), and then all of my queries are done through the relations without caring for the urls. The problem I have is to be able to directly access the page displaying one of my entities.

让我们来接触信息库的例子。如果我从主页开始,然后通过联系人列表浏览,然后选择我要在细节上看到的接触,是没有问题的。

Let's take the example of a contacts repository. If I start from the home page, then navigate through the contact list, then choose the contact I want to see in details, there is no problem.

但我不能直接访问显示联系人的详细信息的页面:资源从列表控制器编辑控制器注入,如果没有被告知编辑控制器无法知道这个请求的URL列表控制器。

But I can't access directly the page showing the details of the contact: the resource is injected from the list controller to the edit controller, and the edit controller is not able to know the url to request if not told by the list controller.

问题的根源是,与春季数据休息,实体有自己的 ID 没有公共领域(不JSON),以及仓库没有API 关于按ID搜索。

The root problem is that with Spring Data Rest, entities have no public field for their id (not in JSON), and the repositories have no API relation to search by id.

我想到了一些解决方案,但我不喜欢任何人。

I thought about some solutions, but I don't like any of them.

1。在后端解决


  • 添加投影与方法的getId()来所有需要的实体进行书签

  • 添加相应的 findById()存储库中的接口

  • 使用ID在前台的URL作为参数(或路径),并通过调用新的可用搜索相关解决资源。

  • 缺点的。这迫使我们手动重做所有的DTO被Spring休息的数据自动生成的,所以我们正在失去的框架的目的有一个

  • 问题的:是那里春天配置为自动揭露那些ID字段和findById方法的一种方式

  • Add a Projection with method getId() to all the entities which needs to be bookmarked
  • Add the corresponding findById() in the repository interface
  • Use the id in the url of the frontend as a parameter (or path) and resolve the resource by calling the new available search relation.
  • Drawbacks : this force us to redo manually all the DTO generated automatically by Spring Data Rest, so we are loosing one of the purpose of the framework.
  • Question : is there a way to configure Spring to automatically expose those id fields and findById methods ?

2。使用自我关系


  • 获取实体的自我URI与角-HAL $的href('自我')

  • 使用它作为页面的参数,并通过调用一个新的解决资源 halClient。$得到(resourceUri)

  • 缺点的:在URI前应放于页面的URL prevent错误beeing后处理由于其他的http://,在您的地址栏。我想它做一些base64编码的,但这种消耗。

  • 缺点的:这暴露了API URL到世界,这个数据可能是至关重要的,需要留隐患(即使这将是通过一个调试器看网络流量访问太)

  • Get the self URI of the entity with angular-hal $href('self') method
  • Use it as a parameter of the page and resolve the resource by calling a new halClient.$get(resourceUri) on it
  • Drawbacks: the uri should be processed before and after beeing put in the page url to prevent errors due to another "http://" in the adress bar. I thought of doing some base64 encoding on it, but this is consuming.
  • Drawbacks: this exposes the API url to the world, and this data could be critical and need to stay hidden (even if this will be accessible through a debugger watching network traffic too).

3。忘记HATEOAS


  • 请不要与关系和发现能力麻烦

  • 删除角-HAL ,只使用 $资源与普通的旧网址映射

  • 缺点的:这是倒退,而且感觉就像没有以下指导...

  • Don't bother with the relations and the discovery capability
  • Removes angular-hal and just use $resource with plain old url mappings
  • Drawbacks: this is going backward, and feels like not following guidelines...

所以,我失去的东西吗?什么是关于数据的完整基于REST HATEOAS环境中接入的最佳做法是什么?

So, am I missing something ? What is the best practice regarding the access of data in a full RESTFul HATEOAS environment ?

推荐答案

我已经找到了 exposeIdsFor 方法,使得可以添加在JSON与@Id注释的领域

I've found the exposeIdsFor methods that makes possible to add the field annotated with @Id in JSON.

@Configuration
public class RepositoryConfiguration  extends SpringBootRepositoryRestMvcConfiguration {

    /**
     * add here the main resources that would need direct access from outside
     */
    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Contact.class, User.class);
    }
}

然后,我把所有的实体我想接触的ID从一个共同的抽象类继承

Then I made all the entities I want the id to be exposed to inherit from a common abstract class

@MappedSuperclass
public abstract class AbstractEntity {

    @Id @GeneratedValue
    Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

这个抽象类是由 @NoRepositoryBean 库服

@NoRepositoryBean
public interface AbstractEntityRepository<T extends AbstractEntity> extends JpaRepository<T, Long> {    
    @RestResource(rel = "byId")
    T findById(@Param("id") Long id);
}

然后我可以公开这两个ID和方法byId()为我所关心的实体:

and then I can expose both id and method byId() for the entities I care :

@Entity
public class Contact extends AbstractEntity {

    @Column 
   String name;

    @Column 
    String email;
}

public interface ContactRepository extends AbstractEntityRepository<Contact> {    
}

我觉得这让周围良好的工作,并允许来自客户机的实体中的一个小的价格直接访问

I think this makes a good work around and allow the direct access of the entities from the client at a small price

这篇关于收藏AngularJS前端(直接访问) - 春天的数据后台休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆