On Tuesday, April 9, 2019 at 12:25:19 PM UTC-7, Paulomi Mukherjee wrote:
>
> Hi All,
>
>
> If we add @JsonTypeInfo annotation to the parent class, is it mandatory 
> that the payload of both the parent class and child class should have 
> "<propertyName> (like @class for ID set as CLASS)" to explicitly specify 
> the class for which the object should be created? 
>

Yes.
 

> Can it not work otherwise?
>

No, with some limitations: omitting of Type Identifier is allowed if 
mapping can use "defaultImpl" type (specific optionally for 
`@JsonTypeInfo`).
But I don't think that matters here.
 

> Can I create a child object on the child endpoint without having the 
> <propertyName> in the payload?
>
> *Issue with example:*
>
> Animal class is parent of Dog class
>
> In Animal.java we have,
>
> @JsonTypeInfo(
>         use = JsonTypeInfo.Id.NAME,
>         include = JsonTypeInfo.As.PROPERTY,
>         property = "@class"
> )
> @JsonSubTypes({
>         @JsonSubTypes.Type(value = com.custom.model.Pet.class, name = 
> "com.custom.model.Pet"),
>         @JsonSubTypes.Type(value = com.custom.model.Dog.class, name = 
> "com.custom.model.Dog"),
>         @JsonSubTypes.Type(value = com.custom.model.Cat.class, name = "
> com.custom.model.Cat"),
>         @JsonSubTypes.Type(value = com.custom.model.rabbit.class, name = 
> "com.custom.model.rabbit")
> })
>
> And Dog.java has
>
> @JsonTypeName("com.custom.model.Dog")
>
> When we create a dog object from its own endpoint by performing post on 
> http://localhost:8080/api/dogs with payload
> {
>        "atr1":"value1"
> }
>
> I get the below error:
>
> 2019-04-08T15:39:46,443 DEBUG [qtp1639085390-18] o.s.c.l.LogFormatUtils: 
> POST "/api/dogs", parameters={}
> 2019-04-08T15:39:46,445 DEBUG [qtp1639085390-18] 
> o.s.w.s.h.AbstractHandlerMapping: Mapped to private java.util.List 
> com.custom.model.DogRestApiController.create(com.custom.model.Dog,javax.servlet.http.HttpServletRequest)
>  
> throws java.lang.Exception
> 2019-04-08T15:39:46,446 DEBUG [qtp1639085390-18] 
> o.s.d.n.w.s.OpenSessionInViewInterceptor: Opening Neo4j OGM Session in 
> OpenSessionInViewInterceptor
> 2019-04-08T15:39:46,450 DEBUG [qtp1639085390-18] 
> o.s.w.m.s.InvocableHandlerMethod: Could not resolve parameter [0] in 
> private java.util.List 
> com.custom.model.DogRestApiController.create(com.custom.model.Dog,javax.servlet.http.HttpServletRequest)
>  
> throws java.lang.Exception: JSON parse error: Missing type id when trying 
> to resolve subtype of [simple type, class com.custom.model.Dog]: missing 
> type id property '@class'; nested exception is 
> com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id 
> when trying to resolve subtype of [simple type, class 
> com.custom.model.Dog]: missing type id property '@class'
> at [Source: (PushbackInputStream); line: 4, column: 1]
> 2019-04-08T15:39:46,452 DEBUG [qtp1639085390-18] 
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver: Using @ExceptionHandler 
> public final org.springframework.http.ResponseEntity<java.lang.Object> 
> org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest)
>  
> throws java.lang.Exception
> 2019-04-08T15:39:46,459 DEBUG [qtp1639085390-18] 
> o.s.w.s.m.m.a.AbstractMessageConverterMethodProcessor: No match for [*/*], 
> supported: []
> 2019-04-08T15:39:46,460 DEBUG [qtp1639085390-18] 
> o.s.w.s.h.AbstractHandlerExceptionResolver: Resolved 
> [org.springframework.http.converter.HttpMessageNotReadableException: JSON 
> parse error: Missing type id when trying to resolve subtype of [simple 
> type, class com.custom.model.Dog]: missing type id property '@class'; 
> nested exception is 
> com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id 
> when trying to resolve subtype of [simple type, class 
> com.custom.model.Dog]: missing type id property '@class'
> at [Source: (PushbackInputStream); line: 4, column: 1]]
> 2019-04-08T15:39:46,461 DEBUG [qtp1639085390-18] 
> o.s.d.n.w.s.OpenSessionInViewInterceptor: Closed Neo4j OGM Session in 
> OpenSessionInViewInterceptor
> 2019-04-08T15:39:46,461 DEBUG [qtp1639085390-18] o.s.w.s.FrameworkServlet: 
> Completed 400 BAD_REQUEST
>
> This exception is caught in spring webmvc. Spring boot version is 2.1.3.
> The control does not reach the custom controller code (the postMapping 
> code) as spring webmvc expects the property name @class from @JsonTypeInfo.
>
>
Yes, that is expected.

There is a feature request to allow omitting Type Id in specific case of 
using specific concrete subtype as target on deserializiation (I don't 
remember github issue id off hand),
so you could do


   Dog dog = mapper.readValue(...., Dog.class);

but not 

   Animal dog = mapper.readValue(...., Animal.class)


but that is not yet supported.

-+ Tatu +-

 

-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to