我有所有国家的休息网址 - http://api.geonames.org/countryInfoJSON?username=volodiaL.
我使用Spring 3中的RestTemplate将返回的json解析为java对象:
RestTemplate restTemplate = new RestTemplate();
Country[] countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",Country[].class);
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到一个例外:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of hello.Country[] out of START_OBJECT token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1846149; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:222)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:133)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
... 7 more
Run Code Online (Sandbox Code Playgroud)
最后我的国家班:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
private String countryName;
private long geonameId;
public String getCountryName() {
return countryName;
} …Run Code Online (Sandbox Code Playgroud) 我有一个POJO课程
public class Stock{
int id;
String name;
Date date;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何注释或开发框架/ api可以将POJO转换为JSON模式,如下所示
{"id":
{
"type" : "int"
},
"name":{
"type" : "string"
}
"date":{
"type" : "Date"
}
}
Run Code Online (Sandbox Code Playgroud)
并且我可以通过在POJO上指定一些注释或配置来扩展模式以添加诸如"必需"之类的信息:"是",每个字段的描述等,并且可以生成如下的JSON模式.
{"id":
{
"type" : "int",
"Required" : "Yes",
"format" : "id must not be greater than 99999",
"description" : "id of the stock"
},
"name":{
"type" : "string",
"Required" : "Yes",
"format" : "name must not be empty and must be 15-30 characters length",
"description" : "name of the …Run Code Online (Sandbox Code Playgroud) 从fasterxml.jackson 2.6.3迁移到2.7.0之后.这是因为public JavaType constructType(Type type, Class<?> contextType)Spring中使用的方法AbstractJackson2HttpMessageConverter被删除了.如何解决这个问题?我使用的是Spring 4.2.3.
/signin/facebook
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType;
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:314)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:146)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:141)
at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:706)
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:770)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)
at org.springframework.social.oauth2.OAuth2Template.postForAccessGrant(OAuth2Template.java:242)
at org.springframework.social.oauth2.OAuth2Template.exchangeForAccess(OAuth2Template.java:144)
at org.springframework.social.connect.web.ConnectSupport.completeConnection(ConnectSupport.java:160)
at org.springframework.social.connect.web.ProviderSignInController.oauth2Callback(ProviderSignInController.java:228)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:44)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
Run Code Online (Sandbox Code Playgroud) 有谁知道com.fasterxml.jackson.databind.ObjectMapper如何能够将JSON属性映射到不区分大小写的POJO属性?
JSON字符串:
[{ "FIRSTNAME": "约翰", "LASTNAME": "Doe的", "DATEOFBIRTH": "1980-07-16T18:25:00.000Z"}]
POJO级:
public class Person {
private String firstName;
private String lastName;
private Date dateOfBirth;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
@Test
public final void testDeserializingPersonJsonToPersonClass()
throws JsonParseException, …Run Code Online (Sandbox Code Playgroud) could not read JSON: Can not construct instance of java.util.Date from String
value '2012-07-21 12:11:12': not a valid representation("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
Run Code Online (Sandbox Code Playgroud)
将json请求传递给POJO类中的REST控制器方法.用户应该只输入以下日期时间格式,否则它应该抛出message.why DateSerializer没有调用?
add(@Valid @RequestBody User user)
{
}
Run Code Online (Sandbox Code Playgroud)
JSON:
{
"name":"ssss",
"created_date": "2012-07-21 12:11:12"
}
Run Code Online (Sandbox Code Playgroud)
pojo类变量
@JsonSerialize(using=DateSerializer.class)
@Column
@NotNull(message="Please enter a date")
@Temporal(value=TemporalType.TIMESTAMP)
private Date created_date;
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
logger.info("serialize:"+value);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logger.info("DateSerializer formatter:"+formatter.format(value));
jgen.writeString(formatter.format(value));
}
Run Code Online (Sandbox Code Playgroud) 我正在为服务器编写一个Json客户端,它将布尔值返回为"0"和"1".当我尝试运行我的Json客户端时,我目前得到以下异常:
HttpMessageNotReadableException: Could not read JSON: Can not construct instance of java.lang.Boolean from String value '0': only "true" or "false" recognized
Run Code Online (Sandbox Code Playgroud)
那么如何设置FasterXML\Jackson来正确解析类似的东西:
{
"SomeServerType" : {
"ID" : "12345",
"ThisIsABoolean" : "0",
"ThisIsABooleanToo" : "1"
}
}
Run Code Online (Sandbox Code Playgroud)
样本Pojo:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"someServerType"})
public class myPojo
{
@JsonProperty("someServerType")
SomeServerType someServerType;
@JsonProperty("someServerType")
public SomeServerType getSomeServerType() { return someServerType; }
@JsonProperty("someServertype")
public void setSomeServerType(SomeServerType type)
{ someServerType = type; }
}
Run Code Online (Sandbox Code Playgroud)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"someServerType"})
public class SomeServerType
{
@JsonProperty("ID")
Integer ID;
@JsonProperty("ThisIsABoolean") …Run Code Online (Sandbox Code Playgroud) 是否可以从中创建流com.fasterxml.jackson.databind.node.ArrayNode?
我试过了:
ArrayNode files = (ArrayNode) json.get("files");
Stream<JsonNode> stream = Stream.of(files);
Run Code Online (Sandbox Code Playgroud)
但它实际上会给出一个元素的流,即初始的ArrayNode对象.
应该是正确的结果Stream<JsonNode>,我可以实现吗?
问题是当将 Spring 缓存与 Redis 缓存管理器一起使用时,由于没有默认构造函数,无法反序列化 Spring Pageable 响应
使用的spring boot版本是2.1.4.RELEASE
使用序列化器的 Redis 配置类
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Spring 缓存和 Redis 作为缓存后端在 Redis 缓存中缓存 Spring REST API 页面结果响应
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
Run Code Online (Sandbox Code Playgroud)
我可以Page<Article>使用序列化程序在 Redis 缓存中看到以 JSON 形式缓存,RedisSerializer.json()但在下一次调用期间,当从缓存中读取数据时,出现以下异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of `org.springframework.data.domain.PageImpl` (no
Creators, like default construct, exist): cannot deserialize from Object
value (no delegate- or …Run Code Online (Sandbox Code Playgroud) fasterxml spring-boot spring-cache spring-data-commons jackson2
我有以下XML架构:
<Courses semester="1">
<Course code="A231" credits="3">Intermediate A</Course>
<Course code="A105" credits="2">Intro to A</Course>
<Course code="B358" credits="4">Advanced B</Course>
</Courses>
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为POJO:
public class Schedule
{
public int semester;
public Course[] courses;
}
public class Course
{
public String code;
public int credits;
public String name;
}
Run Code Online (Sandbox Code Playgroud)
这里有两件重要的事情需要注意:
我如何需要注释我的对象才能让FasterXML反序列化这个xml?
嗨,我使用的Java序列化POJO对象到XML更快的XML(https://github.com/FasterXML/jackson-dataformat-xml/wiki).当我这样做,我得到了XML,但它不具有任何版本和编码在xml文件中.这是我需要的格式
<?xml version="1.0" encoding="utf-8"?>
<SampleRequest>
...
</SampleRequest>
Run Code Online (Sandbox Code Playgroud)
但我得到了这一个
<SampleRequest>
...
</SampleRequest>
Run Code Online (Sandbox Code Playgroud)
是否需要在jackson fasterxml anotation中添加任何配置.
fasterxml ×10
jackson ×6
java ×5
json ×4
spring ×3
xml ×2
data-binding ×1
jackson2 ×1
java-8 ×1
java-stream ×1
jsonschema ×1
pojo ×1
resttemplate ×1
spring-boot ×1
spring-cache ×1
spring-mvc ×1