com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化对象实例出 START_ARRAY 令牌

Posted

技术标签:

【中文标题】com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化对象实例出 START_ARRAY 令牌【英文标题】:com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token 【发布时间】:2017-10-10 22:59:29 【问题描述】:

获取 MismatchedInputException。在这里搜索了很多问题,但主要是发现 JSONMappingException 。我不知道它们是相同的还是不同的。

以下是实体:

@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;

所有的 getter 和 setter 都已就位。

以下是控制器映射函数:

@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);
    

以下是服务:

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

以下是存储库:

@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;
    

以下是我使用的依赖项:

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

我正在通过 Postman 进行测试,我还没有创建前端。以下是我发送的 JSON:

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

最后是控制台日志:

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]

我不确定是什么导致了这个异常。当我启动应用程序时,正在数据库中正确创建表,并且我也在 GET 请求中正确获取数据。

【问题讨论】:

【参考方案1】:

您已将 RequestBody 映射到单个 User 对象,但您发送的是 JSON 数组。

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

您应该发送一个 JSON 对象,如下所示


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

或更改控制器映射以接收User 对象的集合

@RequestBody List<User> users

【讨论】:

以上是关于com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化对象实例出 START_ARRAY 令牌的主要内容,如果未能解决你的问题,请参考以下文章