0

나는 방대한 세계의 백엔드를 처음 접하기 때문에 나와 절한 있습니다. Jackson ObjectMapper를 사용하여 내 항목을 "학생"으로 변환하는 데 문제가 있습니다. 앞에서 보낸 id 매개 변수를 기반으로 올바른 항목을 실제로 얻는 방법을 얻었습니다. 그래서이 방법은 효과가 있지만 아무 것도 반환하지 않습니다. 단지 작동하는지 테스트하고 싶었 기 때문입니다.ID를 기반으로 DynamoDB에서 항목 가져 오기 및 항목을 변환하는 중

AwsService : 그냥 내가거야 참고로

public Student getStudent(String id){ 

    Table t = db.getTable(studentTableName); 

    GetItemSpec gio = new GetItemSpec() 
      .withPrimaryKey("id", id); 

    Item item = t.getItem(gio); 

    //Problem starts here, unsure of how to do. As is, getS() is underlined as error 
    Student student = mapper.readValue(item.get("payload").getS(), Student.class); 

    return student; 
} 

:

public void getStudent(String id){ 

    Table t = db.getTable(studentTableName); 

    GetItemSpec gio = new GetItemSpec() 
      .withPrimaryKey("id", id); 

    Item item = t.getItem(gio); 
    System.out.println("Student: "+item); // <--- Gives the correct item! 

} 

하지만 지금은 그렇게하는 대신 무효의는 "학생"을 반환해야, 그것은 학생을 반환해야 모든 학생을 검색하기위한 제 작업 방법을 추가하십시오. . 당신이 볼 수 있도록로서, 나는 모든 학생들 검색하는 방법과 동일한 mapper.readValue를 사용하려고 :

public List<Student> getStudents() { 

    final List<Student> students = new ArrayList<Student>(); 

    ScanRequest scanRequest = new ScanRequest() 
      .withTableName(studentTableName); 

    ScanResult result = client.scan(scanRequest); 
    try { 
     for (Map<String, AttributeValue> item : result.getItems()) { 
      Student student = mapper.readValue(item.get("payload").getS(), Student.class); 
      students.add(student); 
     } 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 

    return students; 
} 
+0

는 ("페이로드") item.get 교체 "item.getJSON ("페이로드 ")와 함께) (GETS와 어떻게되는지 시도 : 여기에 나를 위해 올바른 방법입니다 문자열 (1).". Dynamodb와 Student 클래스의 모든 속성 이름이 일치하기를 바랍니다! 그렇지 않으면 다른 오류가 발생할 수 있습니다. – notionquest

+0

좋아, 그게 도움이된다! 올바른 데이터를 콘솔에 인쇄하지만 사이에 '\'가 표시됩니다. 오류 코드 92는 다음과 같습니다 : [link] (http://yuluer.com/page/dggeggjd-unexpected-character-code-92-in-jackson.shtml) 나는 "학생"을 위해 escapeJson을 사용하려했지만 밑줄 친 오류를 보았습니다. 탈출구. 어떤 아이디어? – Alex

+0

콘솔 출력의 이스케이프 문자는 JSON 문자열을 인쇄하는 표준 방법이므로 걱정하지 않아도됩니다. 기본 목표는 모든 값이 채워진 Student 객체를 반환하는 것입니다. 대안으로, 이스케이프 문자없이 데이터를 인쇄하는 Student object.toString()을 인쇄 할 수 있습니다. 원래 문제가 해결되면 친절하게 대답하십시오. – notionquest

답변

1

이 item.get ("페이로드") 교체를 (item.getJSON "로) (GETS "payload"). substring (1) ".

+0

의심스러운 백 슬래시로 문제가 발생했습니다. 내 대답 아래. 나는 내 방식으로 언 이스케이프 json을 중첩시켜야했다. – Alex

0

알아 냈습니다. .

public Student getStudent(String id) throws JsonParseException, JsonMappingException, IOException { 

    Table t = db.getTable(studentTableName); 

    GetItemSpec gio = new GetItemSpec() 
      .withPrimaryKey("id", id); 

    Item item = t.getItem(gio); 

    Student student = mapper.readValue(StringEscapeUtils.unescapeJson(item.getJSON("payload").substring(1)), Student.class); 

    return student; 

} 
관련 문제