问题
Resolved By Oliver Gierke's Solution
Looks like this was a known bug in Spring 4.2.0, upgrading to 4.2.1 has provided the expected functionality
Original Question
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.
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
is my dev box,
apps-list/app is where spring-data-rest is bound to (config included below)
Example:
curl jv.local:8080/apps-list/app/departments
Returns:
{
"_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
}
}
} ]
}
}
(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
}
}
}
Here the department has no associated institution listed. Is there a way to enable the institution in _links?
Entity Definitions
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
}
Repositories
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);
}
Configs
Configs are included from a root AppConfig class via @Imports. AppConfig is specified via AbstractAnnotationConfigDispatcherServletInitializer
subclass as a member of getRootConfigClasses()
.
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;
}
}
Version Information
- spring-webmvc, 4.2.0
- spring-context, 4.2.0
- spring-orm, 4.2.0
- spring-data-jpa 1.8.2
- jackson-core 2.6.1
- jackson-databind 2.6.1
- servlet 3.1.0
- spring-data-rest-webmvc 2.3.2
- spring-hateoas 0.18.0
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?
Thanks for your time!
回答1:
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).
来源:https://stackoverflow.com/questions/32386478/spring-data-rest-not-including-entity-links-in-resource