簡體   English   中英

如何使用Spring Data Rest在GET調用中獲取EmbeddedId對象

[英]How to fetch embeddedId object in GET call using Spring Data Rest

我正在使用Spring 1.3.3,並且無法使用GET獲取嵌入式類的關聯對象。 使用投影時,它返回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"
      }
    }
  }

我的域類如下。

Employee.java

@實體

@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

@Entity
@Table(name="address")
public class Address implements Serializable {
    private int id;
    private String streetName;
    private String cityName

Zipcode.java

@Entity
@Table(name = "zipcode")
public class Zipcode implements Serializable {
    @Id
    @GeneratedValue
    private int id;
    private String code;

EmployeeId.java

@Embeddable
public class EmployeeId implements Serializable {
    private Address address;
    private Zipcode zipcode;
    private int empId;

我的預測如下

@Projection(name = "employeeProjection", types = { Employee.class })
public interface EmployeeProjection {

    Address getAddress();

    Zipcode getZipcode();

    int getEmpId();

    String getName();
}

員工搜索存儲庫類如下,

@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);

} 

如何為嵌入式類返回關聯的對象而不是URI?

請提供您的輸入。

您應該在存儲庫定義中使用摘錄。 我可以根據您的GET結果來猜測Employee存儲庫如下:

interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {}

進行如下更改:

@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {} 

這將導致zipCode和地址被內聯。 請注意,指向內聯資源的鏈接仍將出現在投影中。

員工模型應進行修改,並通過嵌入成員上方的@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;

您可以在spring doc中閱讀更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM