gpt4 book ai didi

java - Spring Boot JSON解析错误: Cannot deserialize error

转载 作者:可可西里 更新时间:2023-11-01 08:23:57 25 4
gpt4 key购买 nike

{
"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 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是主键所以不能加

最佳答案

你应该发送一个类似这样的 JSON

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

}

很可能你正在使用 [ 而不是 { 或者两者都用

关于java - Spring Boot JSON解析错误: Cannot deserialize error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com