Java Spring Boot JSON 解析错误:无法反序列化错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51400584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot JSON parse error: Cannot deserialize error
提问by ?mer
{
"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.Hymanson.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/"
}
That's Error Message
那是错误信息
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> {
}
UsersRepository.java
用户存储库.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;
}
}
Users.java
用户.java
Summary;
概括;
At address (localhost:8080/rest/users/all), Get operation is running smoothly. But when I try to create a new User with post at (localhost:8080/rest/users/load), I get error:
地址(localhost:8080/rest/users/all),Get操作运行顺利。但是,当我尝试在 (localhost:8080/rest/users/load) 上创建一个带有 post 的新用户时,出现错误:
"message": "JSON parse error: Cannot deserialize instance of
com.springboot.sprinboot.model.Users
out of START_ARRAY token; nested exception is com.fasterxml.Hymanson.databind.exc.MismatchedInputException: Cannot deserialize instance ofcom.springboot.sprinboot.model.Users
out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
"message": "JSON 解析错误:无法反序列化
com.springboot.sprinboot.model.Users
超出 START_ARRAY 令牌的实例;嵌套异常是 com.fasterxml.Hymanson.databind.exc.MismatchedInputException:无法反序列化com.springboot.sprinboot.model.Users
超出 START_ARRAY 令牌的实例\n 在 [来源:(PushbackInputStream) ; 行: 1, 列: 1]",
example json
示例 json
[
{
"id": 2,
"name": "omer",
"teamName": "omr",
"salary": 200
}
]
Solved
解决了
{
"name": "omer",
"teamName": "omr",
"salary": 200
}
everyone thank you, I was need can't add because id is primary key.
谢谢大家,我需要不能添加,因为id是主键。
回答by Deb
You should send a JSON similar to this
您应该发送类似于此的 JSON
{
"id": 1,
"name": "omer"
........
}
Most probably you are using [
instead of {
or maybe both
很可能你正在使用[
而不是{
或者两者都使用