2016-04-02 129 views
1

我只用球衣,而不是傑克遜創造一個REST API。我有兩個示範對象,自定義對象顯示在JSON

public class Course { 
    private int id; 
    private String name; 
    private Teacher teacher; 
} 

public class Teacher { 

    private int id; 
    private String givenName; 
    private String familyName; 

} 

我創建一個服務,並返回課程對象的名單,

public List<Course> getAll(){ 
     return db.getCourseList(); 
    } 

如預期顯示,

[{"id":101,"name":"Introduction to Java","teacher":{"familyName":"Bar","givenName":"Foo","id":201}},{"id":102,"name":"Intermediate Java","teacher":{"familyName":"Prank","givenName":"Foo","id":202}}] 

現在我想要自定義我的JSON對象,以只顯示以下格式的教師ID。

[{"id":"100","name":"Introduction to Java","teacherId":"201"},{"id":"101","name":"Intermediate Java","teacherId":"201"} 

所以這是我設計的視圖模型。

@XmlRootElement 
public class CourseTeacherIdView { 

    private int id; 
    private String name; 
    private int teacherId; 

    CourseTeacherIdView(){ 

    } 

    public CourseTeacherIdView(int id, String name, int teacherId){ 
     this.id = id; 
     this.name = name; 
     this.teacherId = teacherId; 
    } 

} 

而且我用這個方法返回視圖對象列表。

public List<CourseTeacherIdView> getAll(){ 
     List<Course> list = db.getCourseList(); 
     List<CourseTeacherIdView> viewList = new ArrayList<>(); 
     for(Iterator<Course> itr = list.iterator(); itr.hasNext();){ 
      Course c = (Course) itr.next(); 
      viewList.add(new CourseTeacherIdView(c.getId(), c.getName(), c.getTeacher().getId())); 
     } 
     return viewList; 
    } 

這就是我得到的結果。

[{},{},{}] 

我在做什麼錯。

+0

其實固定它。顯然,如果這個類沒有getter和setter,這就是對象的打印方式。我添加了getters和setters,它工作正常。謝謝。 – Zeus

回答

0

如果我理解正確的,你,你既可以創建新的視圖模型表示,只有ID和列表中的每個對象映射到它,或不相關的領域使用@jsonignore(如果球衣使用傑克遜)。甚至只從數據庫中檢索ID。取決於用例。

+0

請檢查修改。 – Zeus

1

可以實現與傑克遜和創建自定義序列如下所示:

public class CourseSerializer extends JsonSerializer<Course> { 
    @Override 
    public void serialize(Course value, 
          JsonGenerator gen, 
          SerializerProvider serializers) throws IOException { 

     gen.writeStartObject(); 

     Field[] fields = value.getClass().getDeclaredFields(); 

     for (Field field : fields) { 
      field.setAccessible(true); 
      try { 
       Object obj = field.get(value); 
       if (obj instanceof Teacher) { 
        Teacher teacher = (Teacher) obj; 
        gen.writeStringField("teacherId", String.valueOf(teacher.getId())); 
       } else { 
        gen.writeStringField(field.getName(), obj.toString()); 
       } 
      } catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
     } 
     gen.writeEndObject(); 
    } 
} 

測試用例:

public static void main(String[] args) throws Exception { 
    ObjectMapper mapper = new ObjectMapper(); 

    SimpleModule module = new SimpleModule(); 
    module.addSerializer(Course.class, new CourseSerializer()); 
    mapper.registerModule(module); 

    Teacher teacher1 = new Teacher(123, "teacher1", "surename1"); 
    Teacher teacher2 = new Teacher(234, "teacher2", "surename2"); 
    Course course1 = new Course(1, "course1", teacher1); 
    Course course2 = new Course(2, "course2", teacher2); 
    List<Course> courses = Arrays.asList(new Course[]{course1, course2}); 

    String serialized = mapper.writeValueAsString(courses); 
} 

輸出:

[{ 
    "id": "1", 
    "name": "course1", 
    "teacherId": "123" 
}, { 
    "id": "2", 
    "name": "course2", 
    "teacherId": "234" 
}] 
+0

所以我填充我與CourseSerializer對象名單? – Zeus

+0

不,你不改變關於當前列表什麼。您只需要在映射器上註冊序列化程序,並且可以像以前一樣簡單地序列化一系列課程。這是使用傑克遜btw。 – dambros

+0

嗯,我沒有使用傑克遜。 – Zeus