2017-12-28 12 views
0

에 JSON 요청을 변환하지 않았다 그런AWS 람다 내가 람다 함수가 POJO

package org.smarter.note; 

import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.LambdaLogger; 
import com.amazonaws.services.lambda.runtime.RequestHandler; 

public class AddNoteRequestHandler implements RequestHandler<NewNoteRequest, Note> { 

    private NoteRepository notes; 
    private LambdaLogger logger; 

    @Override 
    public Note handleRequest(NewNoteRequest newNote, Context context) { 
     init(context); 

     log("creating note: " + newNote.toJson()); 

     notes.create(newNote); 

     return new Note(); 
    } 

    private void log(String message) { 
     logger.log(String.format("%s\n", message)); 
    } 

    private void init(Context context) { 
     this.logger = context.getLogger(); 
     this.notes = new NoteRepository(); 
    } 
} 

내가 로컬 SAM을 사용하여 람다를 테스트입니다 및 요청 보내

{ 
    "title": "hello", 
    "content": "body" 
} 

I을 json이 NewNoteRequest으로 자동 변환되기를 기대하지만, 그렇지 않습니다. 여기에 로그는 다음과 같습니다

You can now browse to the above endpoints to invoke your functions. 
You do not need to restart/reload SAM CLI while working on your functions, 
changes will be reflected instantly/automatically. You only need to restart 
SAM CLI if you update your AWS SAM template. 

2017/12/28 11:06:57 Invoking org.smarter.note.AddNoteRequestHandler (java8) 
2017/12/28 11:06:57 Decompressing /Work/smarter-serverless/build/distributions/smarter-serverless.zip 
2017/12/28 11:06:57 Mounting /private/var/folders/gv/m43y5g9x1xdc9f2kc2p0kpz00000gp/T/aws-sam-local-1514430417742696978 as /var/task:ro inside runtime container 
START RequestId: bf0f77d3-198d-4211-ab9e-4c85a7c5de22 Version: $LATEST 
creating note: {title=null, content=null} 

내 샘 지역 템플릿 :

AWSTemplateFormatVersion : '2010-09-09' 
Transform: AWS::Serverless-2016-10-31 

Resources: 
    notes: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: org.smarter.note.AddNoteRequestHandler 
     CodeUri: ./build/distributions/smarter-serverless.zip 
     Runtime: java8 
     Events: 
     PostEvent: 
      Type: Api 
      Properties: 
      Path: /notes 
      Method: post 

POJO와 :

package org.smarter.note; 

import com.amazonaws.services.dynamodbv2.document.Item; 

import java.util.HashMap; 
import java.util.Map; 

class NewNoteRequest implements DynamoDBItem, Json { 
    private String title; 
    private String content; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getContent() { 
     return content; 
    } 

    public void setContent(String content) { 
     this.content = content; 
    } 

    @Override 
    public Item item() { 
     return new Item() 
       .withString("title", this.title) 
       .withString("content", this.content); 
    } 

    @Override 
    public Map<String, Object> toJson() { 
     HashMap<String, Object> json = new HashMap<String, Object>(); 
     json.put("title", this.title); 
     json.put("content", this.content); 
     return json; 
    } 
} 

또한 종속성은 다음과 같습니다

dependencies { 
    compile(
      "com.amazonaws:aws-lambda-java-core:1.1.0", 
      "com.amazonaws:aws-lambda-java-events:1.1.0" 
    ) 
} 

내가 알아낼 수 없습니다 어느 부분이 잘못 되었습니까, AWS 문서 그것이 직접 매핑을 할 수 있다고 말한다. 도와주세요.

+0

이 동일한 문제가 있습니다. 실망. 자킴을 해결 했니? 어쨌든 디버깅 할 것 같지 않습니다. API 게이트웨이에서 보낸 JSON 페이로드가 정확하고 자체적으로 람다 함수를 테스트 할 때 동일한 페이로드가 올바르게 작동하는 것을 볼 수 있습니다. POJO는 Lambda에 의해 인스턴스화되지만 set *() 메소드를 호출하지 않아 JSON 페이로드에 전달 된 필드를 설정하지 않습니다. – Mark

+0

@Mark 미안하지만 나는 이것에 운이 없어, 나는 완전히 aws lambda에서 그만 뒀다. – Jakim

+0

감사합니다. @Jakim이 응답했습니다. 미안하지만 람다를 그만 두었습니다.하지만 이해합니다. 매우 직관적이지 않습니다. – Mark

답변

0

이 문제의 제 버전을 해결했습니다. "통합 요청"에서 "Access-Control-Allow-Credentials"를 선택했습니다. 선택을 해제하면 POJO가 채워집니다.