-1

Im creating a RESTful API using Spring-data-rest. I have an entity;

@Entity
@Table(name = "pricingoptionsets") 
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@pricingOptionSetId") //To prevent fetch loops
public class PricingOptionSet {

//Region Properties
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="PricingOptionSetId", nullable=false, unique=true)
private Long pricingOptionSetId;

@ManyToOne
@JoinColumn(name = "ProductId")
private Product product;

Now on the Getters & Setters, If I have the following:

public Product getProducts() {
        return product;
    }

I receive the Product information in the JSON of the response. But if the getter is:

public Product getProduct() {
        return product;
    }

Then Product information is not included anymore:S

Any ideas how to fix that? Btw, I'm using a simple repository that extends CRUDRepository

alexcancode
  • 41
  • 1
  • 1
  • 7
  • what does your setter and json look like? – Scary Wombat Jul 12 '17 at 05:14
  • Thats the JSON{ "_embedded" : { "pricingOptionSets" : [ { "@pricingOptionSetId" : 1, "name" : "AgentRate30", "products" : { "@productId" : 4, "productId" : 1, }, And thats the setter: public void setProduct(Product product) { this.product = product; } – alexcancode Jul 12 '17 at 05:18

2 Answers2

0

Here getProducts() is a method so it doesn't matter weather you define it as getProducts() or getProduct() .

The only problem here which can happen is the implementation where you are using this method should also be changed in accordance with the method name.

Try this or else just post your json response for the same so we can figure out what the problem is but this is sure the method name don't have anything to do with response change.

Amit Gujarathi
  • 1,090
  • 1
  • 12
  • 25
0

As you can see from your json

{ "_embedded" : { "pricingOptionSets" : [ { "@pricingOptionSetId" : 1, "name"
 : "AgentRate30", "products" : { "@productId" : 4, "productId" : 1, }

the property should be products so your need to have the setter and getter to be getProducts setProducts` - Of course it is desirable that the field should also match

You could also consider to use @jsonProperty as per this answer https://stackoverflow.com/a/12583772/2310289

In your case it would be

@JsonProperty(value="products")
private Product product;
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64