6
{
    "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/"
}

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

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

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:

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

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

6
  • 1
    Can you also show the json that you are sending while saving? Commented Jul 18, 2018 at 11:18
  • can you post your json request. Seems that you send an array of users and not a single one. Change your json or your method to accept a list Commented Jul 18, 2018 at 11:18
  • My feeling is that your JSON is malformed. Commented Jul 18, 2018 at 11:19
  • You need to add your json to get the help Commented Jul 18, 2018 at 11:29
  • now, added json Commented Jul 18, 2018 at 13:25

1 Answer 1

7

You should send a JSON similar to this

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

}

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

Sign up to request clarification or add additional context in comments.

1 Comment

yes, i was use both,i need use just " { ", but the source of this error is that id is the primary key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.