2017-02-24 4 views
0

Ich habe zwei Modelle Kategorie und Unterkategorie. Kategorie hat Liste, die eins zu viele Beziehung ist, und wenn ich Daten von DB abrufen, habe ich eine Kategorie hat viele subateogry. Mein Zweck ist es, oben genannte Beziehungsdaten in den JSON-Format-Typ zu erstellen, der im Browser angezeigt wird. Zum Beispiel, Daten wie, List = categoryService.getAllList(); eine Liste der Kategorie zurückgeben, in der auch eine spezifische Liste der Unterkategorie enthalten ist.So ändern Sie ArrayList-Modelle in 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; 
    } 

ErrorLog

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) 

Der Fehler könnte darauf zurückzuführen sein, dass Gson in die Infinity-Schleife gegangen ist und versucht hat, die Kategorieliste in die Unterkategorienliste zu konvertieren.

Bitte helfen Sie mir, dies zu überwinden.

Vielen Dank.

Antwort

1

können Sie @Expose Annotation verwenden. Wie folgt aus:

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; 

Und können Sie Gson wie folgt verwenden:

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

danken Ihnen für Ihre Unterstützung, das funktioniert wirklich für mich. – soewin

0

Sie können Spring Heteos verwenden, um die andere Entität nach Relation zu verknüpfen.

Weitere Details click here