<i id='3PV3A'><tr id='3PV3A'><dt id='3PV3A'><q id='3PV3A'><span id='3PV3A'><b id='3PV3A'><form id='3PV3A'><ins id='3PV3A'></ins><ul id='3PV3A'></ul><sub id='3PV3A'></sub></form><legend id='3PV3A'></legend><bdo id='3PV3A'><pre id='3PV3A'><center id='3PV3A'></center></pre></bdo></b><th id='3PV3A'></th></span></q></dt></tr></i><div id='3PV3A'><tfoot id='3PV3A'></tfoot><dl id='3PV3A'><fieldset id='3PV3A'></fieldset></dl></div>
      <bdo id='3PV3A'></bdo><ul id='3PV3A'></ul>
    <tfoot id='3PV3A'></tfoot>

    <small id='3PV3A'></small><noframes id='3PV3A'>

    1. <legend id='3PV3A'><style id='3PV3A'><dir id='3PV3A'><q id='3PV3A'></q></dir></style></legend>

      1. Spring Data REST 不包括资源中的实体链接

        时间:2024-08-24
      2. <legend id='WC4gm'><style id='WC4gm'><dir id='WC4gm'><q id='WC4gm'></q></dir></style></legend>

        <tfoot id='WC4gm'></tfoot>

                <tbody id='WC4gm'></tbody>

                <small id='WC4gm'></small><noframes id='WC4gm'>

                  <bdo id='WC4gm'></bdo><ul id='WC4gm'></ul>
                  <i id='WC4gm'><tr id='WC4gm'><dt id='WC4gm'><q id='WC4gm'><span id='WC4gm'><b id='WC4gm'><form id='WC4gm'><ins id='WC4gm'></ins><ul id='WC4gm'></ul><sub id='WC4gm'></sub></form><legend id='WC4gm'></legend><bdo id='WC4gm'><pre id='WC4gm'><center id='WC4gm'></center></pre></bdo></b><th id='WC4gm'></th></span></q></dt></tr></i><div id='WC4gm'><tfoot id='WC4gm'></tfoot><dl id='WC4gm'><fieldset id='WC4gm'></fieldset></dl></div>
                  本文介绍了Spring Data REST 不包括资源中的实体链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  看起来这是 Spring 4.2.0 中的一个已知错误,升级到 4.2.1 提供了预期的功能

                  Looks like this was a known bug in Spring 4.2.0, upgrading to 4.2.1 has provided the expected functionality

                  我正在努力将我的开发团队转移到用于 Web 应用程序的 Spring + WebMVC + Data-REST + Data-JPA + Spring HATEOAS.我当前的应用程序只是要维护我们正在进行的应用程序的列表.

                  I'm working on moving my dev team over to Spring + WebMVC + Data-REST + Data-JPA + Spring HATEOAS for web applications. My current app is just going to maintain a list of our ongoing applications.

                  我的默认 Spring Data REST 设置遇到了问题.我的资源未将其链接资源包含在其特定视图中,但它们包含在集合视图中.

                  I'm running into an issue with my default Spring Data REST setup. My resources aren't including their linked resource in their specific views, while they are included in the collection view.

                  我不确定这是否是预期行为,因此我将在本文末尾包含相关配置等.

                  I'm not sure if this is intended behavior or not, so I'll include relevant configs and such at the end of this post.

                  jv.local 是我的开发箱,apps-list/app 是 spring-data-rest 绑定到的地方(包含在下面的配置)

                  jv.local is my dev box, apps-list/app is where spring-data-rest is bound to (config included below)

                  例子:

                  curl jv.local:8080/apps-list/app/departments
                  

                  返回:

                  {
                    "_links" : {
                      "self" : {
                        "href" : "http://jv.local:8080/apps-list/app/departments{?page,size,sort}",
                        "templated" : true
                      }
                    },
                    "_embedded" : {
                      "departments" : [ {
                        "name" : "Dining",
                        "_links" : {
                          "self" : {
                            "href" : "http://jv.local:8080/apps-list/app/departments/1",
                            "templated" : false
                          },
                          "institution" : {
                            "href" : "http://jv.local:8080/apps-list/app/departments/1/institution",
                            "templated" : false
                          }
                        }
                      }, {
                        "name" : "Housing",
                        "_links" : {
                          "self" : {
                            "href" : "http://jv.local:8080/apps-list/app/departments/2",
                            "templated" : false
                          },
                          "institution" : {
                            "href" : "http://jv.local:8080/apps-list/app/departments/2/institution",
                            "templated" : false
                          }
                        }
                      } ]
                    }
                  }
                  

                  (特别注意部门在_links中正确链接了他们的机构)

                  (Note in particular that departments have their institution linked properly in _links)

                  但是,拉一个特定的部门会导致

                  However, pulling a specific department results in

                  curl jv.local:8080/apps-list/app/departments/1
                  {
                    "name" : "Dining",
                    "_links" : {
                      "self" : {
                        "href" : "http://jv.local:8080/apps-list/app/departments/1",
                        "templated" : false
                      }
                    }
                  }
                  

                  这里没有列出该部门的关联机构.有没有办法在_links中启用机构?

                  Here the department has no associated institution listed. Is there a way to enable the institution in _links?

                  Department.java

                  @Entity
                  @Table(name="department")
                  public class Department {
                      @Id
                      @GeneratedValue(strategy = GenerationType.AUTO)
                      private Long id;
                      
                      @Column(name="name")
                      private String name;
                      
                      @ManyToOne(optional = false)
                      @JoinColumn(name="institution", referencedColumnName="id")
                      @RestResource
                      private Institution institution;
                      
                      public Long getId() {
                          return this.id;
                      }
                      
                      public void setId(Long id) {
                          this.id = id;
                      }
                      
                      . . . more getters/setters like above
                  }
                  

                  Institution.java

                  @Entity
                  @Table(name = "institution")
                  public class Institution {
                      @Id
                      @GeneratedValue(strategy = GenerationType.AUTO)
                      private Long id;
                  
                      @Column(name = "name", unique = true)
                      private String name;
                  
                      public Long getId() {
                          return this.id;
                      }
                  
                      public void setId(Long id) {
                          this.id = id;
                      }
                  
                      . . . name getter/setter
                  }
                  

                  存储库

                  DepartmentRepository.java

                  @RestResource(rel="departments",path="departments")
                  public interface DepartmentRepository extends JpaRepository<Department, Long> {
                  }
                  

                  InstitutionRepository.java

                  @RestResource(rel="institutions",path="institutions")
                  public interface InstitutionRepository extends JpaRepository<Institution, Long> {
                      Institution findFirstByName(String name);
                  }
                  

                  配置

                  通过@Imports 从根AppConfig 类中包含配置.AppConfig 通过 AbstractAnnotationConfigDispatcherServletInitializer 子类指定为 getRootConfigClasses() 的成员.

                  Configs

                  Configs are included from a root AppConfig class via @Imports. AppConfig is specified via AbstractAnnotationConfigDispatcherServletInitializer subclass as a member of getRootConfigClasses().

                  AppConfig 类注释如下

                  AppConfig class is annotated with the following

                  @Configuration
                  @ComponentScan({my.packages, my.other.packages})
                  @EnableSpringDataWebSupport
                  @EnableTransactionManagement
                  @EnableJpaRepositories(my.repository.location)
                  @EnableWebMvc
                  @EnableWebSecurity
                  @EnableGlobalMethodSecurity(prePostEnabled = true)
                  @Import({PersistenceConfiguration.class, RestConfiguration.class, MvcConfiguration.class, SecurityConfiguration.class})
                  

                  RestConfiguration.java

                  @Configuration
                  public class RestConfiguration extends RepositoryRestMvcConfiguration {
                      @Override
                      public RepositoryRestConfiguration config() {
                        RepositoryRestConfiguration config = super.config();
                        config.setBasePath("/app");
                        return config;
                      }
                  }
                  

                  版本信息

                  • spring-webmvc, 4.2.0
                  • 弹簧上下文,4.2.0
                  • spring-orm,4.2.0
                  • spring-data-jpa 1.8.2
                  • 杰克逊核心 2.6.1
                  • 杰克逊数据绑定 2.6.1
                  • servlet 3.1.0
                  • spring-data-rest-webmvc 2.3.2
                  • spring-hateoas 0.18.0
                  • 请!让我知道我是否可以提供更多有用的信息,或者可能是一个有效的 GH 项目.如果这是预期行为,是否有任何方法可以覆盖并强制显示链接?

                    Please! let me know if I can provide any more useful information, or possibly a working GH project. If this is intended behavior, is there any way to override, and force links to display?

                    感谢您的宝贵时间!

                    推荐答案

                    这是一个众所周知的 - 谢天谢地已经修复 - Spring 4.2 中的错误.升级到 Spring 4.2.1 应该可以解决这个问题(或 Spring Boot 1.3 M5).

                    That's a well-known – and thankfully already fixed – bug in Spring 4.2. Upgrading to Spring 4.2.1 should fix that (or Spring Boot 1.3 M5).

                    这篇关于Spring Data REST 不包括资源中的实体链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Spring rest 资源变更路径 下一篇:所有嵌入式注释对象的单个自定义序列化程序,用它们的 id 替换它们

                  相关文章

                  1. <legend id='c3795'><style id='c3795'><dir id='c3795'><q id='c3795'></q></dir></style></legend>

                    <small id='c3795'></small><noframes id='c3795'>

                    • <bdo id='c3795'></bdo><ul id='c3795'></ul>
                    <i id='c3795'><tr id='c3795'><dt id='c3795'><q id='c3795'><span id='c3795'><b id='c3795'><form id='c3795'><ins id='c3795'></ins><ul id='c3795'></ul><sub id='c3795'></sub></form><legend id='c3795'></legend><bdo id='c3795'><pre id='c3795'><center id='c3795'></center></pre></bdo></b><th id='c3795'></th></span></q></dt></tr></i><div id='c3795'><tfoot id='c3795'></tfoot><dl id='c3795'><fieldset id='c3795'></fieldset></dl></div>
                    <tfoot id='c3795'></tfoot>