Spring Data Rest 只显示链接但没有信息

问题描述 投票:0回答:2

我在 spring boot 中使用 Spring data rest,所以我编写了代码来为实体使用端点

所以这是我的实体

package com.angelol.ecommerce.entities;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import lombok.Data;

@Entity
@Table(name="f_product")
@Data
public class Product{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "sku")
    private String sku;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "funit_price")
    private BigDecimal unitPrice;

    @Column(name = "image_url")
    private String imageUrl;

    @Column(name = "active")
    private boolean active;

    @Column(name = "date_created")
    @CreationTimestamp
    private Date dateCreated;

    @Column(name = "last_update")
    @UpdateTimestamp
    private Date lastUpdate;

    @ManyToOne
    @JoinColumn(name = "category_id", nullable = false)
    private ProductCategory category;

}

这就是我使用 Spring data rest 的地方

package com.angelol.ecommerce.dao;

import com.angelol.ecommerce.entities.Product;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.CrossOrigin;

@CrossOrigin
@RepositoryRestResource(collectionResourceRel = "product", path = "products")
public interface ProductRepository extends JpaRepository<Product, Long>{ }

所以当我将暴露的控制器用于产品时 (http://127.0.0.1/api/products) 我得到以下 json

{
  "_embedded" : {
    "product" : [ {
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8080/api/products/3"
        },
        "product" : {
          "href" : "http://127.0.0.1:8080/api/products/3"
        },
        "category" : {
          "href" : "http://127.0.0.1:8080/api/products/3/category"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://127.0.0.1:8080/api/products/"
    },
    "profile" : {
      "href" : "http://127.0.0.1:8080/api/profile/products"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

但是没有关于

products
的信息。即使当我尝试获取我拥有的唯一产品的信息时,我也得到了以下 json。

{
  "_links" : {
    "self" : {
      "href" : "http://127.0.0.1:8080/api/products/3"
    },
    "product" : {
      "href" : "http://127.0.0.1:8080/api/products/3"
    },
    "category" : {
      "href" : "http://127.0.0.1:8080/api/products/3/category"
    }
  }
}

所以你可以看到,它只显示

"_links"
但是没有任何信息。我该如何解决?

spring-boot rest spring-data-rest
2个回答
1
投票

我通过删除目标文件解决了我的问题。然后我重新启动了应用程序,它工作了。


0
投票

我试图解决这个问题2天,终于得到了结果。 似乎我们的项目不支持 Lombok 注释,即 @Data 所以,与其将 @Data 放在实体之上,不如使用该实体所有属性的 getter 和 setter,你就得到了结果。

© www.soinside.com 2019 - 2024. All rights reserved.