Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson 2.6.x -> 2.7 deserialize generic fails when calling Spring controller [SPR-14470] #19039

Closed
spring-projects-issues opened this issue Jul 15, 2016 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 15, 2016

Wagner Michael opened SPR-14470 and commented

I'm just trying to upgrade (Spring 4.3 project) from jackson 2.6.3 to 2.7 and getting an error.

This is my simplyfied project:

Abstract controller class with a http post method which accepts a requestBody of a generic list:

public abstract class AbstractController<T extends Entity> {

    @RequestMapping(value = "/", method = POST)
    public void method1(@RequestBody final List<T> entities){
        System.out.println("AbstractController called");
    }
}

Controller implementation with a simple Entity class

@RestController
@RequestMapping("/impl1")
public class ControllerImpl extends AbstractController<EntityImpl> {
}

second controller with method1 overriden

@RestController
@RequestMapping("/impl2")
public class ControllerImpl2 extends AbstractController<EntityImpl2> {

    @Override
    public void method1(@RequestBody List<EntityImpl2> entities) {
        System.out.println("ControllerImpl2 called");
    }
}

My Entity:

public class EntityImpl implements Entity<Integer> {

    private String name;

    public EntityImpl() {
    }

    public EntityImpl(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(final String name) {
        this.name = name;
    }
}

Ok, so this setup works fine with Jackson 2.6.x and Spring 4.2.x, but i need to upgrade to at least 2.7.
But with 2.7 every post request to "/impl1/" gives me this error:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of jackson.entity.Entity, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
     at [Source: java.io.PushbackInputStream@1aa5c56a; line: 1, column: 2] (through reference chain: java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of jackson.entity.Entity, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
     at [Source: java.io.PushbackInputStream@1aa5c56a; line: 1, column: 2] (through reference chain: java.util.ArrayList[0])

Thought posts to /impl2/ workwith both versions ... but i really dont want to override every method there.

Is there a way to solve this?
I have created a sample project with some pretty easy tests showing this behavior.
Just run

./gradlew build

If you open gradle.build and remove these lines:

// uncomment both for failing tests!!
    compile "com.fasterxml.jackson.core:jackson-databind:2.7.0"
    compile "org.springframework:spring-webmvc:4.3.0.RELEASE"

Tests are working fine then. (Jackson 2.6 and Spring 4.2 are being used then).
Am i missing any configuration there, which was not needed prior jackson 2.7?

Hope someone can help me here :)


Affects: 4.3.1

Attachments:

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jul 15, 2016

Sébastien Deleuze commented

Thanks for the repro project! Looks like a regression caused by the switch on our generic type resolution algorithm instead of the Jackson one (not available anymore in Jackson 2.7+) performed as part of #18301. I am going to see how we can fix that.

@spring-projects-issues
Copy link
Collaborator Author

Wagner Michael commented

You're welcome :)
Hope this makes it into 4.3.2, really waiting for this :)

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Fixed by this commit in 4.3.X branch. (I will commit it in master as well tomorrow)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants