使用Jackson

时间:2017-12-29 14:44:54

标签: java json jpa spring-boot jackson

我是Spring Boot,Jackson和JPA的新手。我正在尝试构建一个RESTful应用程序。根据我对Spring Boot文档的理解,反序列化数据和返回JSON的大部分工作都是#34;神奇地"由Spring框架处理(当包含适当的启动器/依赖项时)。

我的控制器端点很简单。

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.midamcorp.roominspections.models.*;
import com.midamcorp.roominspections.service.ItemService;

 @RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

@RequestMapping(method= RequestMethod.GET)
public List<Item> getItems() {
    return itemService.findAll();
}

@RequestMapping(method=RequestMethod.GET, value="/{id}")
public Item getItem(@PathVariable int id) {
    return itemService.findOne(id);
}

@RequestMapping(method=RequestMethod.GET,value="/page/{pageNumber}")
public List<Item> getItemsByPage(@PathVariable int pageNumber) {
    return itemService.findByPage(pageNumber);
}
}

但是,我的类(JPA实体)表现出一些关系。例如:

import java.util.Set;
import javax.persistence.*;

       @Entity
@Table(name="Items")
public class Item {

    public Item() {}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ItemID;

@ManyToOne(optional=false)
@JoinColumn(name="CommentCategoryID", insertable = false, updatable = false)
private CommentCategory commentCategory;

@ManyToOne(optional=false)
@JoinColumn(name="ItemCategoryID", insertable = false, updatable = false)
private ItemCategory itemCategory; 

@OneToMany(mappedBy="item",targetEntity=ReportDetail.class)
private Set<ReportDetail> detail;

private String ItemName;
private String ItemDescription;
private String ItemType;
@Column(name="PageNumber")
private int pageNumber;
private int PossPoints;
private int CommentCategoryID;
private int ItemCategoryID;

// getters and setters follow
}

每当我尝试访问项目端点时,都会收到以下错误:

 Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not deserialize; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not deserialize (through reference chain: java.util.ArrayList[0]->com.midamcorp.roominspections.models.Item["commentCategory"]->com.midamcorp.roominspections.models.CommentCategory["comments"]->org.hibernate.collection.internal.PersistentSet[0]->com.midamcorp.roominspections.models.Comment["reportDetails"])

现在我明白问题在于反序列化数据(通过杰克逊),问题&#34;结束&#34;使用Comment实体的reportDetails属性。但是,我已经检查了Comment和ReportDetails实体,并且没有发现任何错误。

注释

import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name="Comments")
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int CommentID;

public Comment() {}

@OneToMany(mappedBy="comment", targetEntity=ReportDetail.class)
private Set<ReportDetail> reportDetails;

@ManyToOne(optional=false)
@JoinColumn(name="CommentCategoryID", insertable = false, updatable = false)
private CommentCategory commentCategory;

private int CommentCategoryID;
private String CommentBody;

// getters and setters
}

ReportDetail

import javax.persistence.*;

    @Entity
@Table(name="ReportDetailRows")

public class ReportDetail {

    public ReportDetail() {}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ReportRowID;
private int ReportID;
private int ItemID;
private int CommentID;
private int PointsDeducted;
private String ItemIMG;

@ManyToOne(optional=false)
@JoinColumn(name="CommentID", insertable = false, updatable = false)
private Comment comment;

@ManyToOne(optional=false)
@JoinColumn(name="ReportID", insertable = false, updatable = false)
private ReportSummary summary;

@ManyToOne(optional=false)
@JoinColumn(name="ItemID", insertable = false, updatable = false)
private Item item;

// getters and setters
}

使用存储库接口(扩展CrudRepository)和服务层。

以下是一个示例服务:

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.midamcorp.roominspections.models.Item;
import com.midamcorp.roominspections.models.ItemRepo;

   @Service
    @Transactional
    public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemRepo itemRepo;

    @Override
    public List<Item> findAll() {
    return (List<Item>) itemRepo.findAll();
    }

    @Override
    public List<Item> findByPage(int pageNumber) {
    return itemRepo.findByPageNumber(pageNumber);
    }

    @Override
    public Item findOne(int id) {
    return itemRepo.findOne(id);
    }

}

根据我使用.NET的经验,我必须创建DTO(使用诸如AutoMapper之类的库)来展平实体以进行传输。但是,根据我对Jackson文档的回顾,似乎图书馆自动处理这样的扁平化(注意,我发现传输如此大量的数据并不是一个好习惯;因为我是技术新手,我只是想要让它先工作)。如果需要DTO,它们将位于何处?例如,在Controller,Service,Repository等中?

我真的很感激任何建议。我一直在搜索,但一直无法为我的特定问题找到解决方案。

0 个答案:

没有答案