我正在使用Sprint Boot
来实施RESTful
服务。我在Category
和Product
之间有一对多的映射。我的服务工作正常但不知何故我在json中获得了一些额外的json
值,例如handler:{}
和hibernateLazyInitializer:{}
。见下文。
如何摆脱这些价值?
JSON输出:
[
{
"id": 201,
"name": "PowerBook G4 Aluminium",
"description": "PowerBook G4 Aluminium description goes here....",
"listPrice": 800,
"active": "Y",
"category": {
"id": 103,
"name": "Laptops",
"description": "Laptops description goes here....",
"active": "Y",
**"handler": {},
"hibernateLazyInitializer": {}**
}
},
{
"id": 202,
"name": "PowerBook G4 Aluminium",
"description": "PowerBook G4 Aluminium description goes here....",
"listPrice": 1550,
"active": "Y",
"category": {
"id": 107,
"name": "Other",
"description": "Other description goes here....",
"active": "Y",
**"handler": {},
"hibernateLazyInitializer": {}**
}
},
// code removed for brevity
]
Product.java
@Entity
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq")
@SequenceGenerator(name = "product_seq", sequenceName = "product_seq", allocationSize = 1)
@Column(name = "PRODUCT_ID")
private int id;
@Column(name = "NAME")
@NotNull
@NotEmpty
@Size(min = 2, max = 100)
private String name;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "LIST_PRICE")
@Digits(integer = 5, fraction = 2)
private BigDecimal listPrice;
@Column(name = "ACTIVE")
private String active;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
//default constructor
public Product() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getListPrice() {
return listPrice;
}
public void setListPrice(BigDecimal listPrice) {
this.listPrice = listPrice;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return "Product{" + "id=" + id + ", name=" + name + ", description=" + description + ", listPrice=" + listPrice + ", active=" + active + '}';
}
}
Category.java
@Entity
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_seq")
@SequenceGenerator(name = "category_seq", sequenceName = "category_seq", allocationSize = 1)
@Column(name = "CATEGORY_ID")
private int id;
@Column(name = "NAME")
@NotNull
@NotEmpty
@Size(min = 2, max = 100)
private String name;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "ACTIVE")
private String active;
//default constructor
public Category() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
@Override
public String toString() {
return "Category{" + "id=" + id + ", name=" + name + ", description=" + description + ", active=" + active + '}';
}
}
pom.xml依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<!-- DB -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring Boot版本:
<version>1.3.6.RELEASE</version>