gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:20:20 25 4
gpt4 key购买 nike

获取 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 就位。

Controller 映射函数如下:

@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 请求中正确获取数据。

最佳答案

您已将 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"
}

或更改 Controller 映射以接收 User 对象的集合作为

@RequestBody List<User> users

关于java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Can not deserialize instance of object out of START_ARRAY token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923914/

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