jackson jboss eap6.1.1 annotations not working

2019-07-20 03:20发布

I have been trying to get Jackson annotations working in the JBoss RestEasy application on a EAP 6.1.1 server. To be more exact I want to get the JacksonPolymorphicDeserialization working through there annotation like done here.

Now it seems simple enough but for some reason it’s not really working. I got the following code:

package example.model;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type",visible = true)
@JsonSubTypes({@JsonSubTypes.Type(value = ImplementedRequest.class, name = "implementedrequest")})
public abstract class Request {

}

And the implementation of the abstract class:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type", visible = true)
public class ImplementedRequestextends Request {
    @NotEmpty
    private Person applicant;

    private List<AbstractApplicationPart> applications;

}

I created a endpoint that makes a request and processes a request:

@Path("serviceRequest")
@Named("RequestResource")
@RequestScoped
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ValidateRequest
@IgnoreMediaTypes(MediaType.APPLICATION_JSON)
public class RequestResource {
   public Boolean saveRequest( @Valid Request request, @Context HttpServletRequest req) throws JAXBException, AnError {
//Some awesome processing happens here
}
public Response getDummyData() {
// Creates and fills the ImplementedRequestextends
return result;
}
}

Now I also did some configuration and made sure I load the Jackson modules in Jboss with the war deployment. And made sure in my pom I have the same version as the Modules.

My expectation is now that if I do a call I get something like:

{
    "applicant": { "name":"Dummy data", "age":31},
    "applications": [ {"sport":"soccer"},{"sport":"judo"}    ]
}

I was expecting to get a @type name with the field implementedrequest. This is my first attempt; eventually I also want to have the abstract array work like this. But I can't even get this working.

1条回答
贪生不怕死
2楼-- · 2019-07-20 04:01

So in the end I figured out what went wrong.

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

Is the problem. Jackson has 2 versions and both work, but the default for JBoss EAP6.1.1 is the 1.X branch wich is available under org.codehaus. The problem both methods work under the core. It's later moved to a different branch and artifact.

查看更多
登录 后发表回答