我有问题。我正在使用Spring Boot和sqlite3 DB。我试图将数据发送到数据库。 当我向数据库发送数据时,出现此错误:
{
"timestamp": "2019-02-12T12:39:40.413+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize instance of `com.dar.darkozmetika.models.CategoryModel` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.dar.darkozmetika.models.CategoryModel` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
"path": "/api/cateogry/dar"
}
这是我的控制者:
RestController
@RequestMapping("api/cateogry/dar")
public class CategoryController {
@Autowired
private CategoryRepository categoryRepository;
@GetMapping
private List<CategoryModel> getAllCategory (){
System.out.println("sadad");
System.out.println("sadad" + this.categoryRepository.findAll());
return this.categoryRepository.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.OK)
public void create(@RequestBody CategoryModel bike) {
categoryRepository.save(bike);
}
@GetMapping("/{id}")
public CategoryModel getSpecificCategory(@PathVariable("id") long id) {
return null;//categoryRepository.getOne(id);
}
}
这是我的模特
@Entity
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class CategoryModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String categoryName;
private String categoryDescription;
private String imagePath;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
我从邮递员发送了此数据:
[
{
"id": 2,
"categoryName": "dsds",
"categoryDescription": "sdsd",
"imagePath": "Jsdsds"
}
]
非常有趣,我可以从数据库中毫无问题地获取数据。这是我的数据库的退货。
[
{
"id": 1,
"categoryName": "jeff@bikes.com",
"categoryDescription": "Globo MTB 29 Full Suspension",
"imagePath": "Jeff Miller"
}
]
答案 0 :(得分:0)
您的请求不正确,您正在发送CategoryModel
数组,而POST api/cateogry/dar
仅收到CategoryModel
。您应该只发送:
{
"id": 2,
"categoryName": "dsds",
"categoryDescription": "sdsd",
"imagePath": "Jsdsds"
}