0

这是我的书课:

@Entity
@Table(name="book")
public class Book {

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(targetEntity=Category.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="CategoryId")
public Category category;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(length=10)
private int book_id;
@Column(length=128)
private String title;
@Column(length=64)
private String author;
@Column(length=200)
private String description;
@Column(length=10)
private int ISBN;
@Column(length=10)
private float price;
private Date published_Date;
@Lob
@Column
@Basic(fetch = FetchType.LAZY)
private byte[] icon;
//getter and setter
}

这是我的类别课程:

@Entity
@Table(name="category1")
public class Category {
@Id   
@Column(length=12)   
@GeneratedValue(strategy=GenerationType.AUTO)
public int CategoryId;
@Column(length=50)
public String CategoryName;

//@JsonBackReference
@OneToMany(mappedBy="category")
private List<Book> books = new ArrayList<Book>();
//getter and setter
}

它们之间的关系是一对多的。这是我的类别服务类

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
@Autowired
private CategoryDao dao;

@Autowired
private BookDao dao1;

@Override
public List<Category> getAllCategory(){
    return dao.findAll();
}
}

我的控制器类

@RestController
@RequestMapping("/bookstore")
public class CategoryController {
@Autowired
private AdminService service;

@GetMapping("/GetAllCategory")
private ResponseEntity<List<Category>> getAllCategory() {
    List<Category> catlist = service.getAllCategory();
    return new ResponseEntity<List<Category>>(catlist, new HttpHeaders(), HttpStatus.OK);

}
}

我的类别表已经有数据。当我尝试显示它们时,它显示的是双值。 使用 Postman 显示值 数据库中的类别表:数据库表

4

1 回答 1

0

Jackson's ObjectMapper uses the Java bean pattern and it expects the following

public class Foo {
    public Object bar;

    public Object getBar() {...}

    public void setBar(Object bar) {...}
}

The getters and setters start with get and set, respectively, followed by the corresponding field name with its first letter capitalized.

Change

CategoryId to categoryId (first letter lowercase)

and

CategoryName to categoryName

于 2020-07-17T11:34:35.290 回答