获取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请求中正确获取数据。
答案 0 :(得分:5)
您已将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