2017-02-24 175 views
0

我有兩個模型Category和SubCategory。 類別有List,它是一對多的關係,當我從DB獲取數據時,我得到了一個類別有很多subcateogry。我的目的是將上述關係數據創建爲JSON格式類型以在瀏覽器上顯示。例如,獲取數據,如List = categoryService.getAllList();返回一個類別列表,其中還包括子類別的特定列表。如何將ArrayList模型更改爲JSON

Category.java

@Entity 
public class Category implements Serializable { 

    private static final long serialVersionUID = -5774598582410455895L; 

    @Id 
    @GeneratedValue 
    private int categoryId; 

    @NotEmpty(message = "The category name must not be empty") 
    @Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed") 
    private String categoryName; 

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    private List<SubCategory> subCategory; 
//getter setter 

SubCategory.java

@Entity 
public class SubCategory implements Serializable { 


    private static final long serialVersionUID = 7750738516036520962L; 

    @Id 
    @GeneratedValue 
    private Integer subCategoryId; 

    @NotEmpty(message = "The subcategory name must not be empty") 
    @Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed") 
    private String subCategoryName; 

    @ManyToOne 
    @JoinColumn(name="categoryId") 
    private Category category; 
//getter setter 

ControllerSample.java

@RequestMapping(value="/prodList.json") 
    public @ResponseBody String sectionList(ModelMap modelMap) throws JsonProcessingException{ 
      Gson gson = new Gson(); 
      String jsondata = gson.toJson(categoryService.getAllCategroy()); 
      System.out.println("jsondata = " + jsondata); 

    return jsondata; 
    } 

錯誤日誌

at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:113) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:240) 
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:950) 
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:113) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:240) 
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) 

錯誤可能是由於Gson進入了Infinity循環並試圖在子類別列表中轉換類別列表。

請幫我解決這個問題。

非常感謝。

回答

1

可以使用@expose註釋。就像這樣:

Category.java

@Entity 
public class Category implements Serializable { 
    private static final long serialVersionUID = -5774598582410455895L; 

    @Id 
    @GeneratedValue 
    @Expose 
    private int categoryId; 

    @NotEmpty(message = "The category name must not be empty") 
    @Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed") 
    @Expose 
    private String categoryName; 

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @Expose 
    private List<SubCategory> subCategory; 

SubCategory.java

@Entity 
public class SubCategory implements Serializable { 

    private static final long serialVersionUID = 7750738516036520962L; 

    @Id 
    @GeneratedValue 
    @Expose 
    private Integer subCategoryId; 

    @NotEmpty(message = "The subcategory name must not be empty") 
    @Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed") 
    @Expose 
    private String subCategoryName; 

    @ManyToOne 
    @JoinColumn(name="categoryId") 
    private Category category; 

你也可以使用GSON這樣的:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 
String jsondata = gson.toJson(categoryService.getAllCategroy()); 
System.out.println("jsondata = " + jsondata); 
+0

感謝您的支持,這還真管用爲了我。 – soewin

0

您可以使用Spring Heteos通過關係鏈接另一個實體。

詳情click here

相關問題