To avoid this exception, pass String from the Aura JavaScript to the Apex class and use JSON.deSerialize() to convert the String to wrapper class list in the Apex Class.
Sample Code with Error:
Component:
<aura:component implements=”force:appHostable”
controller=”AccountListController”>
<lightning:button label=”Click” onclick=”{!c.callCheckValues}”></lightning:button>
</aura:component>
JavaScript:
({
callCheckValues : function( component, event, helper ) {
var listWrappers = [];
listWrappers.push( { “strName” : “Test”, “strAddress” : “Testing 123” } );
console.log( ‘Wrapper List is ‘ + JSON.stringify( listWrappers ) );
var action = component.get( “c.checkValues” );
action.setParams({
“listWrappers” : listWrappers
});
action.setCallback( this, function( response ) {
console.log( ‘Response is ‘ + JSON.stringify( response.getReturnValue() ) );
});
$A.enqueueAction( action );
}
})
Apex Class:
public class AccountListController {
@AuraEnabled
public static List < Wrapper > checkValues( List < Wrapper > listWrappers ) {
try {
for ( Wrapper objWrap : listWrappers ) {
system.debug( ‘Name is ‘ + objWrap.strName );
system.debug( ‘Address is ‘ + objWrap.strAddress );
}
system.debug( ‘Wrapper is ‘ + listWrappers );
return listWrappers;
} catch( Exception e ) {
system.debug( ‘Exception is ‘ + e.getMessage() );
return null;
}
}
public class Wrapper {
@AuraEnabled
public String strName { get; set; }
@AuraEnabled
public String strAddress { get; set; }
public Wrapper() {
strName = ”;
strAddress = ”;
}
public Wrapper( String strname, String strAddress ) {
strName = this.strName;
strAddress = this.strAddress;
}
}
}
Sample Code with Fix:
JavaScript:
({
callCheckValues : function( component, event, helper ) {
var listWrappers = [];
listWrappers.push( { “strName” : “Test”, “strAddress” : “Testing 123” } );
console.log( ‘Wrapper List is ‘ + JSON.stringify( listWrappers ) );
var action = component.get( “c.checkValues” );
action.setParams({
“strWrapper” : JSON.stringify( listWrappers )
});
action.setCallback( this, function( response ) {
console.log( ‘Response is ‘ + JSON.stringify( response.getReturnValue() ) );
});
$A.enqueueAction( action );
}
})
Apex Class:
public class AccountListController {
@AuraEnabled
public static List < Wrapper > checkValues( String strWrapper ) {
List < Wrapper > listWrappers = ( List < Wrapper > )JSON.deSerialize( strWrapper, List < Wrapper >.class );
try {
for ( Wrapper objWrap : listWrappers ) {
system.debug( ‘Name is ‘ + objWrap.strName );
system.debug( ‘Address is ‘ + objWrap.strAddress );
}
system.debug( ‘Wrapper is ‘ + listWrappers );
return listWrappers;
} catch( Exception e ) {
system.debug( ‘Exception is ‘ + e.getMessage() );
return null;
}
}
public class Wrapper {
@AuraEnabled
public String strName { get; set; }
@AuraEnabled
public String strAddress { get; set; }
public Wrapper() {
strName = ”;
strAddress = ”;
}
public Wrapper( String strname, String strAddress ) {
strName = this.strName;
strAddress = this.strAddress;
}
}
}