I retrieve JSON from an API:
public with sharing class BeerRequests {
@AuraEnabled
public static BeerJsonParser getRandomBeer() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://prost.herokuapp.com/api/v1/beer/rand');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
BeerJsonParser suggestedBeer = BeerJsonParser.parse(response.getBody());
return suggestedBeer;
}
return null;
}
I parse it to an Apex object using a generated class BeerJsonParser
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
public with sharing class BeerJsonParser {
public class Definition {
public String key;
public String title;
}
public String key;
public String title;
public String synonyms;
public String abv;
public String srm;
public String og;
public List<String> tags;
public Definition brewery;
public Definition country;
public static BeerJsonParser parse(String json) {
return (BeerJsonParser) System.JSON.deserialize(json, BeerJsonParser.class);
}
}
The JSON is being retrieved (server side) correctly and an Apex object is created as expected.
I want to show this data in my lightning app.
<aura:component controller="BeerRequests">
<aura:attribute name="beerinfo" type="BeerJsonParser" />
<aura:handler name="init" action="{! c.doInit }" value="{! this }" />
<ui:outputText value="{! v.beerinfo.title }" />
</aura:component>
And use the following init to load the data
({
doInit: function(component, event, helper) {
var action = component.get("c.getRandomBeer");
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
component.set("v.beerinfo", response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
The response in the controller (client side) is valid and state is success. But the getReturnValue
function (client side) returns an empty object instead of the expected object. This object is NOT empty on the server side.
Question: Why am I unable to return the BeerJsonParser
object to my lightning app? I did not create a custom object BeerJsonParser
in the SalesForce Object Manager.
BeerJsonParser
is not aura enabled