简体   繁体   English

如何使用 @EmbeddedId 使用 Spring Data REST 和 ConversionService?

[英]How to use Spring Data REST and ConversionService using @EmbeddedId?

I have an application with Spring Data REST that returns this JSON:我有一个带有 Spring Data REST 的应用程序,它返回此 JSON:

{
  "_embedded" : {
    "persons" : [ {
      "personDetail" : {
        "name" : "Alex",
        "surname" : "Red",
        "id" : {
          "group" : "A",
          "subclass" : "1"
        },
        "_links" : {
          "self" : {
            "href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
          }
        }
      }
     }]
    }
}   

When I go to the url:当我转到网址时:

https ://localhost:8080/myApp/api/personDetails/A_1 https://localhost:8080/myApp/api/personDetails/A_1

or to this url:或到这个网址:

https ://localhost:8080/myApp/api/persons/04ee99a5-1578-400a-84be-d1ca87cda752/personDetail https://localhost:8080/myApp/api/persons/04ee99a5-1578-400a-84be-d1ca87cda752/personDetail

The app returns this JSON:该应用程序返回此 JSON:

{
  "name" : "Alex",
  "surname" : "Red",
  "_links" : {
    "self" : {
      "href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
    },
    "personDetail" : {
      "href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
    }
  }
}

The "id" field seems to be disappeared. “id”字段似乎消失了。 Where is finished?哪里完成了? How can I do to have the correct object projection?我该怎么做才能获得正确的对象投影?

This is the Person Class:这是人类:

@Entity
@Table
public class Person {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)", length = 16)
    private UUID id;

    @ManyToOne(fetch = FetchType.EAGER)
    private PersonDetail personDetail;

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public PersonDetail getPersonDetail() {
        return personDetail;
    }

    public void setPersonDetail(PersonDetail personDetail) {
        this.personDetail = personDetail;
    }
}

This is the PersonDetail Class:这是 PersonDetail 类:

@Entity
@Table
public class PersonDetail {
    @EmbeddedId
    private PersonDetailId id;

    @Column
    private String name;

    @Column
    private String surname;

    public PersonDetailId getId() {
        return id;
    }

    public void setId(PersonDetailId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    protected String[] getExcludeFieldNames() {
        return null;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toStringExclude(this, getExcludeFieldNames());
    }
}

This is PersonDetailId class:这是 PersonDetailId 类:

public class PersonDetailId implements Serializable {

    @Column(name = "group", nullable = false)
    private String group;

    @Column(name = "subclass", nullable = false)
    private String subclass;

    public PersonDetailId() {
        super();
    }

    public PersonDetailId(String group, String subclass) {
        super();
        this.group = group;
        this.subclass = subclass;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getSubclass() {
        return subclass;
    }

    public void setSubclass(String subclass) {
        this.subclass = subclass;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(group).append("_").append(subclass);
        return builder.toString();
    }

}

This is the repository REST:这是存储库 REST:

@RepositoryRestResource
public interface PersonDetailRepository extends JpaRepository<PersonDetail, PersonDetailId> {

    @RestResource(exported=false)
    PersonDetail findBySurname(String surname);

}

This is the Converter that I used:这是我使用的转换器:

@Component
public class PersonDetailIdConverter implements Converter<String, PersonDetailId> {

    @Autowired
    private PersonDetailRepository personDetailRepository;

    @Override
    public PersonDetailId convert(String source) {

        PersonDetailId result = null;

        List<PersonDetail> details = personDetailRepository.findAll();
        for (PersonDetail detail:details) {
            if (detail.getId().toString().equals(source)) {
                result = detail.getId();
                break;
            }
        }

        return result;
    }

}

And this is the configuration that registers that converter:这是注册该转换器的配置:

@Configuration
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

    @Bean
    PersonDetailIdConverter personDetailIdConverter(){
        return new PersonDetailIdConverter();
    }

    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        conversionService.addConverter(personDetailIdConverter());
        super.configureConversionService(conversionService);
    }

}

I used the converter because it's the only way to make working the url:我使用转换器,因为它是使 url 工作的唯一方法:

"href" : "https ://localhost:8080/myApp/api/personDetails/A_1" "href" : "https://localhost:8080/myApp/api/personDetails/A_1"

Any idea?有什么想法吗? Thanks.谢谢。

EDIT编辑

It seems that it depends by projections.似乎这取决于预测。 When I go to the link of the object using the projection that I have created, so the returned JSON contains all the values that I need.当我使用我创建的投影转到对象的链接时,返回的 JSON 包含我需要的所有值。

Because of this issue where ResourceSupport.getId() has a @JsonIgnore annotation on it, Spring Data REST will not export the IDs of your entities.由于ResourceSupport.getId()上有@JsonIgnore注释的这个问题,Spring Data REST 不会导出实体的 ID。 You can however rename them eg to personId (or just rename your getter method).但是,您可以将它们重命名为personId (或仅重命名您的 getter 方法)。

Also remember to expose your IDs , as Alessandro C mentioned.还记得公开你的 ID ,正如 Alessandro C 提到的那样。

From spring 3.1 onwards, this is how to register the converter:从 spring 3.1 开始,这是如何注册转换器:

@Import(RepositoryRestMvcConfiguration.class)
public class CustomizedRestMvcConfiguration implements RepositoryRestConfigurer {

    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        conversionService.addConverter(new PersonDetailIdConverter());
        super.configureConversionService(conversionService);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM