这个问题与这个问题有关(Spring boot @ResponseBody没有序列化实体id).我观察到,在将应用程序迁移到Spring Boot并使用spring-boot-starter-data-rest依赖项后,我的实体@Id字段不再在生成的JSON中进行编组.
这是我的请求映射,在调试时,我可以看到数据在返回之前没有被更改,因此@Id属性稍后被剥离.
@RequestMapping(method = RequestMethod.GET, produces = {"application/json"})
public PagedResources<Receipt> receipts(Pageable pageable, PagedResourcesAssembler assembler) {
Page<Receipt> receipts = receiptRepository.findByStorerAndCreatedDateGreaterThanEqual("003845", createdStartDate, pageable);
PagedResources<Receipt> pagedResources = assembler.toResource(receipts, receiptResourceAssembler);
return pagedResources;
}
Run Code Online (Sandbox Code Playgroud)
是否有一个设置允许我在生成的JSON中保留@Id字段,因为我的应用程序允许用户按该值进行搜索.
谢谢 :)
我想使用Spring Rest接口公开所有ID.我知道默认情况下这样的ID
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private Long id;
Run Code Online (Sandbox Code Playgroud)
不会通过其余界面公开.
我知道我可以用它
@Configuration
public class RepositoryConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(User.class);
}
}
Run Code Online (Sandbox Code Playgroud)
公开用户的ID.
但有没有一种简单的方法来公开我的所有ID而无需手动维护此configureRepository方法中的列表?
提前致谢!
我曾经暴露了在实体中用@Id注释的主键.ID字段只在资源路径上可见,但在JSON主体上不可见.
我有一个实体
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样初始化它
@PostConstruct
public void init() {
List<String> newFiles = this.listFiles();
newFiles.forEach(filename -> {
Book book = …Run Code Online (Sandbox Code Playgroud)