Whats the issue with generating a concrete type that would have fields for
your interface, and then you would have that model class implement your
interface? You would still need to provide a concrete type to deserialize
to, but later down the chain you would reference to your object only by
interface.

On Fri, Aug 19, 2022 at 6:06 AM Daniel Young <[email protected]>
wrote:

> Here is my code:
>
> ```java
> import com.fasterxml.jackson.core.JsonProcessingException;
> import com.fasterxml.jackson.core.type.TypeReference;
> import com.fasterxml.jackson.databind.ObjectMapper;
>
> public class TestJackson {
>
>     static interface Entity {
>         String getName();
>     }
>
>     @Test
>     public void test() throws JsonProcessingException {
>
>         String json = "{\"name\": \"good\"}";
>
>         ObjectMapper objectMapper = new ObjectMapper();
>         Entity entity = objectMapper.readValue(json, Entity.class);
> //        Entity entity = objectMapper.readValue(json, new
> TypeReference<Entity>(){});
>
>         System.out.println("name: " + entity.getName());
>     }
> }
> ```
> Get exception:
> ```java
> Exception in thread "main"
> com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
> construct instance of `io.gitlab.donespeak.turtal.TestJackson$Entity` (no
> Creators, like default construct, exist): abstract types either need to be
> mapped to concrete types, have custom deserializer, or contain additional
> type information
>  at [Source: (String)"{"name": "good"}"; line: 1, column: 1]
>     at
> com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
>     at
> com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589)
>     at
> com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055)
>     at
> com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
>     at
> com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
>     at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
>     at
> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
>     at io.gitlab.donespeak.turtal.TestJackson.test(TestJackson.java:23)
>     at io.gitlab.donespeak.turtal.TestJackson.main(TestJackson.java:10)
>
> Process finished with exit code 1
> ```
>
> I think dynamic proxy could be a solution for deserializing json to a
> interface.
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jackson-user/9776d0ca-bd05-4d6b-98fe-711012e3703fn%40googlegroups.com
> <https://groups.google.com/d/msgid/jackson-user/9776d0ca-bd05-4d6b-98fe-711012e3703fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
// Mantas

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAE9hW8QMX5wviwzmsesUPfW4tDPgD1Piq0XL%3DSrmZea8iVCTkg%40mail.gmail.com.

Reply via email to