带有泛型

时间:2016-05-23 22:19:33

标签: java spring generics jackson

我在使用Spring MVC 4.2.6和Jackson(2.7.3)时遇到了一些问题。我创建了一个absctract @RestController类,其方法使用泛型类型。我实现了这个抽象类传递泛型对象的类型。我使用@RequestBodyDispatcherServlet抛出JsonMappingException(无法构造我的类的实例)。已配置应用程序上下文中的MappingJackson2HttpMessageConverter

修改:添加了更多信息。

public abstract class AbsctractAnimalResource<DTO extends AnimalDTO> {

    // Doesn't work. Throws JsonMappingException. (@RequestBody)
    @RequestMapping(value = "/bar", method = POST, produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<?> bar(@RequestBody DTO dto) throws Exception {
        return new ResponseEntity(dto, HttpStatus.CREATED);
    }

}

@RestController
@RequestMapping("/cat")
public class CatResource extends AbsctractAnimalResource<CatDTO> {

}

@RestController
@RequestMapping("/dog")
public class DogResource extends AbsctractAnimalResource<DogDTO> {

}


interface AnimalDTO { }
public class CatDTO implements AnimalDTO { }
public class DogDTO implements AnimalDTO { }

1 个答案:

答案 0 :(得分:0)

在Spring 4.2.6类中进行了一些调试后,我发现了这个评论:

https://github.com/spring-projects/spring-framework/blob/4.2.x/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java#L51

Spring 4.2.X和Jackson 2.7.X不能一起工作。我将我的杰克逊版本降级到2.6.6,现在一切正常。 Spring 4.3.X适用于Jackson 2.7 +。