由 Oliver Gierke 的解决方案解决
看起来这是 Spring 4.2.0 中的一个已知错误,升级到 4.2.1 已提供预期的功能
<小时/>原始问题
我正在努力将我的开发团队转移到 Spring + WebMVC + Data-REST + Data-JPA + Spring HATEOAS 来开发 Web 应用程序。我当前的应用程序只是维护我们正在进行的应用程序的列表。
我的默认 Spring Data REST 设置遇到了问题。 我的资源未将其链接资源包含在其特定 View 中,但它们包含在 Collection View 中。
我不确定这是否是预期行为,因此我将在本文末尾添加相关配置等。
jv.local
是我的开发盒,
apps-list/app 是 spring-data-rest 绑定(bind)的地方(配置如下)
示例:
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中正确链接了其机构)
但是,拉动特定部门会导致
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 中启用该机构?
实体定义
部门.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()
的成员。
AppConfig 类注释如下
@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
- Spring 上下文,4.2.0
- spring-orm,4.2.0
- spring-data-jpa 1.8.2
- jackson 核心 2.6.1
- jackson 数据绑定(bind) 2.6.1
- servlet 3.1.0
- spring-data-rest-webmvc 2.3.2
- spring-hateoas 0.18.0
请!如果我可以提供任何更多有用的信息,或者可能是一个可行的 GH 项目,请告诉我。如果这是预期的行为,是否有任何方法可以覆盖并强制显示链接?
感谢您的宝贵时间!
最佳答案
这是一个众所周知的问题 - 幸运的是已经修复了 - bug in Spring 4.2 。升级到 Spring 4.2.1 应该可以解决这个问题(或 Spring Boot 1.3 M5)。
关于java - Spring Data REST 不包括资源中的实体链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386478/