2017-02-24 174 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

相关问题