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

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

问题描述

我正在使用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没有默认(无arg)构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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