CustomDeserializer 没有默认(无参数)构造函数 [英] CustomDeserializer has no default (no arg) constructor

查看:48
本文介绍了CustomDeserializer 没有默认(无参数)构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 RestTemplate 的 REST Api.我从 API 得到的响应有很多嵌套对象.这里有一个小片段作为例子:

I am consuming a REST Api with RestTemplate. The response I'm getting from the API has lots of nested objects. Here's a little snippet as an example:

"formularios": [
  {
    "form_data_id": "123006",
    "form_data": {
      "form_data_id": "123006",
      "form_id": "111",
      "efs": {
        "1": {},
        "2": "{"t":"c","st":"m","v":[{"id":"3675","l":"a) Just an example","v":"1"},{"id":"3676","l":"b) Another example.","v":"2"}]}"
      }
    }

我遇到的问题是大多数时候1"实际上有内容,就像2"一样,而jackson只是将其解析为对象efs"上的字符串.但有时,就像在代码片段中一样,API 将其发送为空,而 jackson 将其作为一个对象,这给了我一个错误,说明了 START_OBJECT(不记得确切的错误,但对于这个问题并不重要)).

The problem I'm having is that most of the times the "1" actually has content, just like "2", and the jackson just parses it as a String on the object "efs". But sometimes, just like in the code snippet, the API sends it empty, and jackson takes it as an Object, which gives me an error that says something about START_OBJECT (can't remember the exact error, but it's not important for this question).

所以我决定制作一个自定义反序列化器,这样当 jackson 读取1"时,它会忽略空对象并将其解析为空字符串.

So I decided to make a custom deserializer so when jackson reads "1", it ignores the empty object and just parses it as a null string.

这是我的自定义解串器:

Here's my custom deserializer:

public class CustomDeserializer extends StdDeserializer<Efs> {

 public CustomDeserializer(Class<Efs> t) {
     super(t);
 }

 @Override
 public Efs deserialize(JsonParser jp, DeserializationContext dc)
         throws IOException, JsonProcessingException {

     String string1 = null;
     String string2 = null;
     JsonToken currentToken = null;

     while ((currentToken = jp.nextValue()) != null) {
         if (currentToken.equals(JsonToken.VALUE_STRING)) {
             if (jp.getCurrentName().equals("1")) {
                 string1 = jp.getValueAsString();
             } else {
                 string2 = jp.getValueAsString();
             }

         } else {
             if (jp.getCurrentName().equals("2")) {
                 string2 = jp.getValueAsString();
             }

         }
     }
     return new Efs(string1, string2);

  }
 }

这是我在从 API 接收响应时使用它的方式:

And this is the way I'm using it when receiving the response from the API:

    ObjectMapper mapper = new ObjectMapper();  
    SimpleModule mod = new SimpleModule("EfsModule");
    mod.addDeserializer(Efs.class, new CustomDeserializer(Efs.class));
    mapper.registerModule(mod);


    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(mapper);
    messageConverters.add(jsonMessageConverter);


    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(messageConverters);

我收到错误:

 CustomDeserializer has no default (no arg) constructor

但我不知道我做错了什么,也不知道如何解决.感谢您对这么长的问题的帮助和道歉,我想提供尽可能多的背景信息.

But I don't know exactly what I'm doing wrong nor how to solve it. Thanks for the help and apologies for the long question, I wanted to give as much context as possible.

推荐答案

要求你有一个没有参数的默认构造函数.您可以做的是创建一个(如果您真的不需要,则替换另一个):

It is required that you have a default constructor without arguments. What you can do is create one (or replace the other one if you don't really need it):

public class CustomDeserializer extends StdDeserializer<Efs> {

   public CustomDeserializer() {
       super(Efs.class);
   }
   ...
}

这篇关于CustomDeserializer 没有默认(无参数)构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆