- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
获取 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/
我的应用使用 Jackson。我得到的最小化构建主要使用此配置: # don't obfuscate Jackson classes -keep class com.fasterxml.** { *;
编辑:在我问这个问题的那一刻,我想到了尝试一些事情..我已经根据请求设置了 XmlNamespace 属性,这就成功了.. request.XmlNamespace = "http://musicbr
我使用可序列化和可反序列化的泛型。但是,Deserialize上有错误派生属性告诉无法推断类型。 struct 和 enum 都会抛出编译错误。评论其中之一不会解决任何问题。 use serde::{
passport.socketio 抛出此错误,同时无法授权用户。 Error: Error: Failed to deserialize user out of session 我已将问题范围缩小到
我正在尝试在具有借用内容的结构上派生反序列化: #[macro_use] extern crate serde_derive; use std::net::SocketAddr; #[derive(H
我执行查询以创建下面的Hive表: CREATE TABLE db1.test_create_tbl( column1 smallint COMMENT 'desc of column') COMME
这个问题在这里已经有了答案: Can System.Text.Json.JsonSerializer serialize collections on a read-only property? (
我正在使用 event_emmiter_rs用于我的应用程序中的事件处理。该库允许您订阅带有回调的事件并触发这些事件。事件采用 (strings, value) 的形式,回调采用接受值参数的闭包形式。
我收到一个错误...(不存在像默认构造函数这样的创建者):无法从对象值反序列化(没有基于委托(delegate)或基于属性的创建者),这表明我需要一个属性基于的创作者。我有几个具有不同参数但没有默认值
我相信我们需要一个自定义的反序列化器来对我们类的一个字段做一些特定的事情。看来一旦我这样做了,我现在负责反序列化所有其他领域。有没有办法让 jackson 反序列化除了我在这里关心的领域之外的所有领域
我有一个如下所示的类(class) class Person { Long id; String firstName; int age; } 我的输入看起来像这样: { "id
文档建议 NancyFx 帮助我解决 json 请求正文的 WRT 反序列化,但我不确定如何。请参阅下面的测试以演示: [TestFixture] public class ScratchNancy
考虑代码... using System; using System.Text.Json; public class Program { public static void Main()
有人让Deserializer工作吗?我正在方法“反序列化”而不是元素中获得完整的JSON表达式? public static void main(String[] args) { G
在尝试从 tokio TcpStream 反序列化为 JSON Value 时,我正在尝试使用此函数: use futures::prelude::*; use serde_json::Value;
我使用 Spring Boot 和 thymeleaf我尝试保存对象列表。 我的对象。 public class GECPD { public Integer id; public S
我正在尝试制作在线领唱。我需要从外部API获取包含货币值的表,正是从该页面:http://api.nbp.pl/api/exchangerates/tables/A?format=json我想在货币课
错误: java.lang.ClassNotFoundException: testprocedure.tp$3 at java.net.URLClassLoader$1.run(Unknown So
我开发了多个 GWT Web 应用程序。最新的一个是对另一个的稍微修改。除了最新的之外,所有这些都运行良好。异常(exception)是: The response could not be dese
我需要反序列化 1.5GB txt 文件。我正在使用 code.google.com/p/protobuf-net/中的 protobuf-net 有时它会因不同地方的不同异常(空引用、内存访问冲突)
我是一名优秀的程序员,十分优秀!