Jpatel Jpatel - 3 months ago 36
AngularJS Question

How to send multiple objects from Angular2 to Java Spring backend

I am trying to send two objects (

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

Here is my code:


CustProduct class:

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


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, body, options)

Backend code:

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

other additional fields

public class Product {
private long PRODUCT_ID;
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
object. If I send
object in
by itself, it works but not in the wrapper object. My class definitions on GUI and backend are same.

Thanks for you help.


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.)