AngeLOL
AngeLOL

Reputation: 116

Spring Data Rest only shows links but no information

I am using Spring data rest with spring boot, so I wrote the code to use endpoints for an entity

So this is my entity

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;

}

And this is where I am using 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>{ }

So when I use the exposed controller for products (http://127.0.0.1/api/products) I get the following 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
  }
}

But theres no information about the products. Even when I try to get information of the only product I have I got the following 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"
    }
  }
}

So you can see, it only shows "_links" but there's no information. How can I fix it?

Upvotes: 1

Views: 1204

Answers (2)

harsh
harsh

Reputation: 11

I was trying to solve this problem for 2 days and finally, I got the result. Seems like our IDE does not support the Lombok annotation which is @Data so, Instead of putting @Data on top of the entity just use the getter and setter of all the attributes of that entity and you got the result.

Upvotes: 1

Yuri
Yuri

Reputation: 277

I solved my problem by deleting the target file. Then I restarted the application and it worked.

Upvotes: 1

Related Questions