2017-12-09 3 views
0

Android 모바일 앱 클라이언트에서 AWS Lambda 함수 (서버리스 백엔드)를 호출하려고합니다. AWS 람다 함수는 POJO 객체의 ArrayList (JSON)를 반환합니다.Amazon Lambda에서 POJO의 ArrayList를 가져 오는 방법 (LinkedTreeMap 만 가져 오기)

문제는 안드로이드 클라이언트 AWS 람다 (JSON) DataBinder가 POJO의 내 ArrayList에 비 직렬화되지 않는다는 것입니다. LinkedTreeMap의 ArrayList를 얻습니다 (아래 onPostExecute() 코드 참조). com.amazonaws : AWS-안드로이드 SDK 코어 : 나는 안드로이드 AWS SDK를 사용하고 안드로이드 클라이언트 측에서

다음
public void readSurveyList(String strUuid, int intLanguageID) { 

    // Create an instance of CognitoCachingCredentialsProvider 
    // You have to configure at least an AWS identity pool to get access to your lambda function 
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
      this.getApplicationContext(), 
      IDENTITY_POOL_ID, 
      Regions.EU_CENTRAL_1); 

    LambdaInvokerFactory factory = LambdaInvokerFactory.builder() 
      .context(this.getApplicationContext()) 
      .region(Regions.EU_CENTRAL_1) 
      .credentialsProvider(credentialsProvider) 
      .build(); 

    // Create the Lambda proxy object with default Json data binder. 
    myInterface = factory.build(MyInterface.class); 

    //create a request object (depends on your lambda function) 
    SurveyListRequest surveyListRequest = new SurveyListRequest(strUuid, intLanguageID); 

    // Lambda function in async task with definiton of 
    //  request object (-> SurveyListRequest) 
    //  response object (-> ArrayList<SurveyListItem>>) 
    new AsyncTask<SurveyListRequest, Void, ArrayList<SurveyListItem>>() { 
     @Override 
     protected ArrayList<SurveyListItem> doInBackground(SurveyListRequest... params) { 

      try { 
       return myInterface.ReadSurveyList(params[0]); 
      } catch (LambdaFunctionException lfe) { 
       Log.e("TAG", String.format("echo method failed: error [%s], details [%s].", lfe.getMessage(), lfe.getDetails())); 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(ArrayList<SurveyListItem> surveyList) { 

      // PROBLEM: here i get a ArrayList of LinkedTreeMap 

     } 
    }.execute(surveyListRequest); 
} 

코드입니다 : 여기에 2.6

몇 가지 코드 내 람다 함수의 인터페이스 :

public interface MyInterface { 

    @LambdaFunction 
    ArrayList<SurveyListItem> ReadSurveyList (SurveyListRequest surveyListRequest); 
} 

내 POJO 개체의 목록을 얻을 것으로 예상됩니다. Gson과 ArrayList 유형 및 TypeToken (예 : Gson TypeToken with dynamic ArrayList item type)을 기반으로 한 솔루션에 대해 많은 논의가있었습니다. 아마 같은 문제가 ...

답변

0

맞춤형 LambdaDataBinder를 사용하여 해결책을 찾았습니다. deserialize 함수에서 내 POJO 클래스 "SurveyListItem"의 유형을 지정했습니다. Gson은 TypeToken 정의를 사용하고 올바른 JSON 문자열을 POJO 목록 (내 경우 "SurveyListItem"객체)으로 변환합니다.

다음
public class MyLambdaDataBinder implements LambdaDataBinder { 

    private final Gson gson; 
    Type mType; 

    //CUSTOMIZATION: pass typetoken via class constructor 
    public MyLambdaDataBinder(Type type) { 
     this.gson = new Gson(); 
     mType = type; 
    } 

    @Override 
    public <T> T deserialize(byte[] content, Class<T> clazz) { 
     if (content == null) { 
      return null; 
     } 
     Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content))); 

     //CUSTOMIZATION: Original line of code: return gson.fromJson (reader, clazz); 
     return gson.fromJson(reader, mType); 
    } 

    @Override 
    public byte[] serialize(Object object) { 
     return gson.toJson(object).getBytes(StringUtils.UTF8); 
    } 
} 

사용자 정의 MyLambdaDataBinder를 사용하는 방법은 다음과 같습니다

는 MyLambdaDataBinder의 소스 코드입니다. "SurveyListItem"대신 POJO를 사용하십시오 :

myInterface = factory.build(LambdaInterface.class, new MyLambdaDataBinder(new TypeToken<ArrayList<SurveyListItem>>() {}.getType())); 
관련 문제