0

I am trying to send two objects (CustomerAcct and Product) from Angular 2 to restful services (Java Spring). I am using a wrapper object called CustProduct, which has two objects in it, CustomerAcct and Product. Somehow on the back end CustomerAcct object is null but Product object is not.

Here is my code:

GUI:

CustProduct class:

export class CustProduct { public ca: CustomerAcct; public prod: Product }

Service:

addProductToCustomer(cp: CustProduct): Observable<Boolean> {

     let body = JSON.stringify(cp);
     let headers = new Headers({'Content-Type': 'application/json'});
     let options = new RequestOptions({ headers: headers });
     let url = 'my-url';

     return this.http.post(my-url, body, options)
                     .map(this.extractData)
                     .catch(this.hadnleError);
}

Backend code:

java classes:
@Entity
@Table(name="CUSTOMER_ACCT")
public class CustomerAcct {
    @Id
@Column(name="CUSTOMER_ACCT_ID")
private long CUSTOMER_ACCT_ID;
@Column (name="CUSTOMER_NAME")
private long CUSTOMER_NAME;

other additional fields
}

@Entity
@Table(name="PRODUCT")
public class Product {
@Id
@Column(name="PRODUCT_ID")
private long PRODUCT_ID;
@Column(name="PROD_DESC")
private String PROD_DESC;

other additional fields
  }

public class CustomerProduct {
CustomerAcct customerAcct;
Product product;
}

public boolean addProductToCustomer(@RequestBody CustomerProduct cp) {
       CustomerAcct ca = cp.customerAcct;
       Product prod = cp.product;

       // Here CustomerAcct is null, but I do see data in Product object
}

I don't understand why it is dropping CustomerAcct object. If I send CustomerAcct object in @RequestBody by itself, it works but not in the wrapper object. My class definitions on GUI and backend are same.

Thanks for you help.

5
  • 1
    Java class definitions? Commented Oct 11, 2016 at 17:23
  • @chrylis Entity Table(name="CUSTOMER_ACCT") public class CustomerAcct { id Column(name="CUSTOMER_ACCT_ID") private long CUSTOMER_ACCT_ID; Column (name="CUSTOMER_NAME") private long CUSTOMER_NAME; } Entity Table(name="PRODUCT") public class Product { Id Column(name="PRODUCT_ID") private long PRODUCT_ID; Column(name="PROD_DESC") private String PROD_DESC; } public class CustomerProduct { CustomerAcct customerAcct; Product product; } Commented Oct 11, 2016 at 17:43
  • I am using '@'Column, '@'Id @Table annotations in the class, but the here it is not allowing me to add '@' sign here. Hope you get my point. Thanks Commented Oct 11, 2016 at 17:45
  • Edit them into your question, and it should work fine. Commented Oct 11, 2016 at 17:57
  • @chrylis Thanks for putting up with me. I am new to this site. I just edited in my questions. Commented Oct 11, 2016 at 18:00

1 Answer 1

1

You're using a simple JSON conversion, which means that in the JSON object your keys are ca and prod. By default, Jackson expects the keys to match the field names, which are customerAcct and product. The simplest approach is probably to annotate your Java fields with @JsonField("ca"). (Alternately, you could rename the properties in either Java or JavaScript.)

Sign up to request clarification or add additional context in comments.

1 Comment

@Jpatel If you find an answer helpful, you can upvote it with the arrow next to it. If an answer solves your problem, you can accept it by selecting the check mark under the arrows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.