弹簧靴使用投影

时间:2018-01-31 10:45:53

标签: java spring rest spring-boot projection

我使用弹簧启动和弹簧数据。 在REST应用程序中,我使用以下格式调用我的GET Web服务" http://localhost:8080/project/api/persons"我通过仅显示与fisrtname和lastname相关的信息,而不是与他有关的对象,例如地址(地址是我项目中的另一个实体)来获取数据库中所有人的列表。 因此,当我使用以下链接调用#34; http://localhost:8080/project/api/persons1?Projection=resumePerson"时,我使用了投影来显示与地址相关的信息。我会寻求结果。但是,通过以下链接" http://localhost:8080/project/api/persons?Projection=resumePerson"地址信息总是不可见的。

 public class Person {
  @Id
  private Long id;

  private String lastname;

  private String firstname;

  @OneToMany
  List<Address> address;

}

public class Address {
  @Id
  private Long id;

  private String rue;

  private String code;


  private String country;

}

@RepositoryRestResource(collectionResourceRel = "persons", path = "persons", excerptProjection = ResumePerson.class)
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

}

@Projection(name = "resumePerson", types = { Person.class })
interface ResumePerson {
    String getFirstname();

    String getLastname();

    List<Address> getAddress();
}

GET网络服务的结果。 http://localhost:8080/project/api/personshttp://localhost:8080/project/api/persons?projection=resumePerson

{
  "_embedded" : {
    "persons" : [ {
      "firstname" : "firstname_1",
      "lastname" : "lastname_1",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/si-ice/api/persons/1{?projection}",
          "templated" : true
        },
        "address" : {
          "href" : "http://localhost:8080/si-ice/api/agents/1/address"
        },
      }
    },
    {
      "firstname" : "firstname_2",
      "lastname" : "lastname_2",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/si-ice/api/persons/2{?projection}",
          "templated" : true
        },
        "address" : {
          "href" : "http://localhost:8080/si-ice/api/agents/2/address"
        },
      }
    }
    ]
  }
}

http://localhost:8080/project/api/persons/1?projection=resumePerson

{
      "firstname" : "firstname_1",
      "lastname" : "lastname_1",
      "Address" : {
          "rue" : "RUE_1",
          "code" : "CODE_1",
          "country" : "COUNTRY_1"
      },          
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/si-ice/api/persons/1{?projection}",
          "templated" : true
        },
        "address" : {
          "href" : "http://localhost:8080/si-ice/api/agents/1/address"
        },
      }
    }

0 个答案:

没有答案