Spring Boot JSON解析错误:无法反序列化错误

编程入门 行业动态 更新时间:2024-10-28 08:27:18
本文介绍了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> { }

UsersRepository.java

UsersRepository.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

Users.java

摘要;

在地址(localhost:8080/rest/users/all)上,Get操作正在顺利运行. 但是,当我尝试使用(localhost:8080/rest/users/load)上的帖子创建新用户时,出现错误:

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:

"message":"JSON解析错误:无法从START_ARRAY令牌中反序列化com.springboot.sprinboot.model.Users的实例;嵌套的异常是com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_ARRAY令牌中反序列化com.springboot.sprinboot.model.Users的实例\ n位于[来源:(PushbackInputStream);第1行,第1列;,

"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]",

示例json

[ { "id": 2, "name": "omer", "teamName": "omr", "salary": 200 } ]

已解决

{ "name": "omer", "teamName": "omr", "salary": 200 }

所有人,谢谢,我不需要添加,因为id是主键.

everyone thank you, I was need can't add because id is primary key.

推荐答案

您应该发送与此类似的JSON

You should send a JSON similar to this

{ "id": 1, "name": "omer" ........ }

很可能您使用的是[而不是{或两者都使用

Most probably you are using [ instead of { or maybe both

更多推荐

Spring Boot JSON解析错误:无法反序列化错误

本文发布于:2023-11-26 19:41:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   序列化   Spring   Boot   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!