[英]How to fetch embeddedId object in GET call using Spring Data Rest
I am using Spring 1.3.3 and I am unable to get the associated object for embedded class using GET. 我正在使用Spring 1.3.3,并且无法使用GET获取嵌入式类的关联对象。 It returns the URI instead of object while using projection.
使用投影时,它返回URI而不是对象。
{
"employeeId": {
"empId": 4,
"_links": {
"address": {
"href": "http://localhost:8082/address/1"
},
"zipcode": {
"href": "http://localhost:8082/zipcode/2"
}
}
},
"empId": 4,
"name": "ap",
},
"_links": {
"self": {
"href": "http://localhost:8082/employee/com.blah.blah.Employee@64f11498"
},
"adNetworkParam": {
"href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498{?projection}",
"templated": true
},
"demandSource": {
"href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498/address"
},
"targetCharacteristic": {
"href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498/zipcode"
}
}
}
My domain classes as follows.. 我的域类如下。
Employee.java Employee.java
@Entity @实体
@Table(name = "employee")
public class Employee implements Serializable {
private EmployeeId employeeId;
private Address address;
private Zipcode zipcode;
private int empId;
private String name;
//getter
...
@MapsId("address")
@ManyToOne
@JoinColumn(name = "address_id")
public Address getAddress() {
return address;
}
@MapsId("zipcode")
@ManyToOne
@JoinColumn(name = "zipcode_id")
public Zipcode getZipcode() {
return zipcode;
}
// setter
.....
Address.java Address.java
@Entity
@Table(name="address")
public class Address implements Serializable {
private int id;
private String streetName;
private String cityName
Zipcode.java Zipcode.java
@Entity
@Table(name = "zipcode")
public class Zipcode implements Serializable {
@Id
@GeneratedValue
private int id;
private String code;
EmployeeId.java EmployeeId.java
@Embeddable
public class EmployeeId implements Serializable {
private Address address;
private Zipcode zipcode;
private int empId;
My Projection is as follows, 我的预测如下
@Projection(name = "employeeProjection", types = { Employee.class })
public interface EmployeeProjection {
Address getAddress();
Zipcode getZipcode();
int getEmpId();
String getName();
}
Employee Search repository class as follows, 员工搜索存储库类如下,
@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {
List<Employee> findByNameContaining(@Param("name") @RequestParam("name") String name);
Employee findById(@Param("employeeId") @RequestParam("employeeId") Integer employeeId);
}
How to return associated object instead of URI for embedded class?? 如何为嵌入式类返回关联的对象而不是URI?
Kindly provide your inputs. 请提供您的输入。
You should use excerpts in your repository definition. 您应该在存储库定义中使用摘录。 I can guess according to your GET result, that the Employee repository is as following:
我可以根据您的GET结果来猜测Employee存储库如下:
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {}
Change it as following: 进行如下更改:
@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {}
This will cause to zipCode and address to be inlined. 这将导致zipCode和地址被内联。 Note, that links to inlined resources will still appear in the projection.
请注意,指向内联资源的链接仍将出现在投影中。
The employee model should be modified and to be defined with @OneToOne annotation above embedded members: 员工模型应进行修改,并通过嵌入成员上方的@OneToOne注释进行定义:
@Table(name = "employee")
public class Employee implements Serializable {
private EmployeeId employeeId;
@OneToOne
private Address address;
@OneToOne
private Zipcode zipcode;
private int empId;
private String name;
You can read more in spring doc . 您可以在spring doc中阅读更多内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.