标签: fasterxml

无法读取JSON:无法反序列化START_OBJECT令牌中的hello.Country []实例

我有所有国家的休息网址 - 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)

java spring resttemplate fasterxml

37
推荐指数
2
解决办法
19万
查看次数

从java类生成JSON模式

我有一个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)

json jsonschema jackson fasterxml jackson-modules

37
推荐指数
3
解决办法
6万
查看次数

Spring 4.2.3和rapidxml Jackson 2.7.0是不兼容的

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

spring jackson fasterxml

31
推荐指数
1
解决办法
3万
查看次数

不更改POJO的不区分大小写的JSON到POJO映射

有谁知道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)

java data-binding json case-insensitive fasterxml

30
推荐指数
6
解决办法
3万
查看次数

JSON以自定义格式序列化日期(无法从String值构造java.util.Date的实例)

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 spring-mvc jackson fasterxml

27
推荐指数
2
解决办法
6万
查看次数

如何从FasterXML\Jackson序列化/反序列化布尔值作为Int?

我正在为服务器编写一个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)

java spring json jackson fasterxml

19
推荐指数
2
解决办法
4万
查看次数

从ArrayNode创建Java 8流

是否可以从中创建流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>,我可以实现吗?

jackson java-8 fasterxml java-stream

17
推荐指数
3
解决办法
9605
查看次数

PageImpl 反序列化期间出错:无法构造“org.springframework.data.domain.PageImpl”的实例

问题是当将 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

13
推荐指数
2
解决办法
2万
查看次数

如何使用FasterXML使用注释反序列化XML

我有以下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)

这里有两件重要的事情需要注意:

  1. 课程对象不包含在标签中
  2. 一些属性是属性

我如何需要注释我的对象才能让FasterXML反序列化这个xml?

java xml pojo jackson fasterxml

11
推荐指数
1
解决办法
1万
查看次数

FasterXML jackson-dataformat-xml序列化版本和编码未添加到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中添加任何配置.

java xml fasterxml

11
推荐指数
3
解决办法
8157
查看次数