Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token

Getting the MismatchedInputException. Searched a lot of questions here but found JSONMappingException mostly. I don't understand if they are same or different.

Following is the entity:

@Entity
@Table
@NamedQueries({
    @NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
    @NamedQuery(name="User.findByEmail", query="SELECT u FROM User u WHERE u.email=:pEmail")
})
public class User {

@Id
@GenericGenerator(name = "idGenerator", strategy = "uuid2")
@GeneratedValue(generator = "idGenerator")
private String id;
private String firstName;
private String lastName;

@Column(unique=true)
private String username;

@Column(unique=true)
private String email;
private String password;
private String role;
}

with all the getters and setters in place.

Following is the controller mapping function:

@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User create(@RequestBody User user) {
        return service.create(user);
    }

Following is the service:

@Override
    @Transactional
    public User create(User user) {
        User existing = repository.findByEmail(user.getEmail());
        if(existing == null){
            repository.create(user);
        }
        return user;
    }

Following is the repository:

@Override
    public User findByEmail(String email) {
        TypedQuery<User> query = em.createNamedQuery("User.findByEmail", User.class);
        query.setParameter("pEmail", email);
        List<User> users = query.getResultList();
        if(users != null && users.size()==1){
            return users.get(0);
        }
        return null;
    }

    @Override
    public User create(User user) {
        em.persist(user);
        return user;
    }

Following are the dependencies I used:

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0.pr2</version>
        </dependency>
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.9.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

I am testing through Postman, I have not yet created the front end. Following is the JSON I am sending:

[{
    "firstName": "hgf",
    "lastName": "frew",
    "username": "erf",
    "email": "bgghjk",
    "password": "bgte",
    "role": "trrere"
  }
]

Lastly below is the console log:

Thu May 11 13:56:43 CDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
May 11, 2017 1:57:52 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotReadable
WARNING: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of priyanka.movieflix.entity.User out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of priyanka.movieflix.entity.User out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1]

I am not sure what is causing this exception. When I start the application, table is being created in the database correctly and I am getting the data correctly in GET request as well.

like image 802
priyanka singh Avatar asked May 11 '17 19:05

priyanka singh


1 Answers

You have mapped the RequestBody to a single User object but you are sending in JSON array.

[{
    "firstName": "hgf",
    "lastName": "frew",
    "username": "erf",
    "email": "bgghjk",
    "password": "bgte",
    "role": "trrere"
  }
]

You should send a JSON object instead as follows

{
    "firstName": "hgf",
    "lastName": "frew",
    "username": "erf",
    "email": "bgghjk",
    "password": "bgte",
    "role": "trrere"
}

or change the controller mapping to receive a collection of User objects as

@RequestBody List<User> users
like image 76
Master Po Avatar answered Nov 02 '22 12:11

Master Po