首页 > 技术文章 > SpringBoot中返回Json(@Data,@JsonProperty(“ID”),org.springframework.beans.BeanUtils)

gzhli 2018-03-16 22:21 原文


@Data  注解可以不用写get,set方法

@JsonProperty(“ID”) 可以让Controller层返回给Json格式时可以用

例子:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;
@Data 
public class ProductInfoVO {
@JsonProperty(
"id")
private String productId;
@JsonProperty(
"name")
private String productName;
@JsonProperty(
"price")
private BigDecimal productPrice;
@JsonProperty(
"description")
private String productDescription;
@JsonProperty(
"icon")
private String productIcon;
}

//显示结果
{
  "id": "1234568",
  "name": "巧克力派",
  "price": 5,
  "description": "非常好吃",
  "icon": "http://sss.jpg"
 }
//快速把ProductInfo对象里的数据传给我们要传给前台的ProductInfo Json格式对象。
BeanUtils.copyProperties(productInfo,productInfoVO);
 

 

推荐阅读