【问题标题】:Spring Boot JSON parse error: Cannot deserialize errorSpring Boot JSON解析错误:无法反序列化错误
【发布时间】:2018-12-26 06:56:26
【问题描述】:
{
    "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

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

总结;

在地址(localhost:8080/rest/users/all),Get操作运行顺利。 但是当我尝试在(localhost:8080/rest/users/load)创建一个新用户时,我收到错误:

"message": "JSON 解析错误:无法从 START_ARRAY 令牌中反序列化 com.springboot.sprinboot.model.Users 的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化 com.springboot.sprinboot.model.Users 的实例\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?
  • 你能发布你的json请求吗?似乎您发送了一组用户而不是一个用户。更改您的 json 或您的方法以接受列表
  • 我的感觉是你的 JSON 格式不正确。
  • 您需要添加您的 json 以获得帮助
  • 现在,添加了 json

标签: java mysql json spring-boot


【解决方案1】:

您应该发送一个类似于此的 JSON

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

}

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

【讨论】:

  • 是的,我两者都用,我只需要使用“{”,但这个错误的根源是 id 是主键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
相关资源
最近更新 更多