我正在创建执行 CRUD 操作的程序“广告目录”。我需要运行方法 save(Ad ad),Ad 包含参数本地日期。我通过 ReSTLet 输入 JSON 代码来获取结果,但它给出错误 500(本地日期错误)。如何使本地日期正确?
我尝试写成“date”:“2012-03-19T07:22Z”,“date”:“2019-09-19”,“date”:2019-09-19,根本不写,但它不起作用。
@Entity
public class Ad {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ad_id")
private int id;
private String name;
private LocalDate date;
private String text;
@Column(name = "price", precision = 10, scale = 2)
private BigDecimal price;
@ManyToOne
@JoinColumn(name = "author_fk_id")
private Author author;
@ManyToOne
@JoinColumn(name = "category_fk_id")
private Category category;
.....
}
@RestController
@RequestMapping("ad")
public class AdController {
@Autowired
@Qualifier("adServiceImpl")
private AdService<Ad> adService;
@PostMapping("/save")
public void save(@RequestBody Ad ad) {
adService.save(ad);
}
}
JSON
{
"id": 0,
"name": "house for sale",
"text": "selling a house in barry ave, 5 ears old",
"date": 2019-09-20,
"price": 250.00,
"author": {
"id": 0,
"name": "Liza",
"phone": {
"id": 0,
"number": "3121001111"
},
"address": {
"id": 0,
"country": "RUSSIA",
"city": "MOSCOW"
},
"email": {
"id": 0,
"email": "liza@mail.ru"
}
},
"category": {
"id": 0,
"name": "houses"
}
}
不要使用私有(private) LocalDate 日期;使用 java.sql.date 类型并以 json 格式传递日期,如下所示 "date": 2019-09-20,
它将起作用,如果您想将该日期格式转换为其他本地日期格式,请使用 SimpleDateFormat 并解析它。
我是一名优秀的程序员,十分优秀!