java - Spring Boot JSON解析错误:无法反序列化错误
问题描述
{
"timestamp": "2018-07-18T11:02:29.789+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize instance of `com.springboot.sprinboot.model.Users` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.springboot.sprinboot.model.Users` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
"path": "/rest/users/"
}
那是错误信息
package com.springboot.sprinboot.resource;
import com.springboot.sprinboot.model.Users;
import com.springboot.sprinboot.repository.UsersRepository;
import org.apache.tomcat.util.http.parser.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import java.awt.*;
import java.util.List;
@RestController
@RequestMapping(value = "/rest/users")
public class UsersResource {
@Autowired
UsersRepository usersRepository;
@GetMapping(value = "/all")
public List<Users> getAll(){
return usersRepository.findAll();
}
@PostMapping (value = "/load")
public List<Users> persist(@RequestBody final Users users){
usersRepository.save(users);
return usersRepository.findAll();
}
}
UsersResource.java
package com.springboot.sprinboot.repository;
import com.springboot.sprinboot.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UsersRepository extends JpaRepository<Users, Integer> {
}
用户存储库.java
package com.springboot.sprinboot.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Users {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "team_name")
private String teamName;
@Column (name = "salary")
private Integer salary;
public Users() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}
用户.java
概括;
在地址(localhost:8080/rest/users/all),Get操作运行顺利。但是当我尝试在(localhost:8080/rest/users/load)创建一个新用户时,我收到错误:
"message": "JSON 解析错误:无法反序列化
com.springboot.sprinboot.model.Users
超出 START_ARRAY 令牌的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法com.springboot.sprinboot.model.Users
在 [Source: (PushbackInputStream) 处反序列化超出 START_ARRAY 令牌的实例\n ; 行:1,列:1]",
示例 json
[
{
"id": 2,
"name": "omer",
"teamName": "omr",
"salary": 200
}
]
解决了
{
"name": "omer",
"teamName": "omr",
"salary": 200
}
谢谢大家,我需要无法添加,因为 id 是主键。
解决方案
您应该发送一个类似于此的 JSON
{
"id": 1,
"name": "omer"
........
}
很可能您正在使用[
而不是{
或两者兼而有之
推荐阅读
- javascript - 如何在 JavaScript 中制作水波纹/闪闪发光的动画?
- python - Spacy - 保存自定义管道
- arrays - 如何在 Scala 中将字节数组划分为块?
- perforce - 如何在不修改本地文件的情况下取消跟踪 Perforce 更改列表中的更改(P4V 2015.2)?
- ruby-on-rails - 在使用端口 8080 进行登台时设计错误的重定向
- c++ - 连接到 IRC 服务器 C++ Windows 10 Visual Studio
- jquery - $(" 之间的区别“) 和 ”"
- image - 是否存在更快地将图像连接到视频的任何快速方法?
- r - 如何使用 R 中一组 pdf 文件中的文本创建数据框?
- javascript - 如何获得与 Materialize.css 框架自动完成的重叠