I have the following object model in my Spring MVC (v3.2.0.RELEASE) web application:

public class Order {
  private Payment payment;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT)
@JsonSubTypes.Type(name = "creditCardPayment", value = CreditCardPayment.class)
public interface Payment {}


@JsonTypeName("creditCardPayment")
public class CreditCardPayment implements Payment {}

当我将Order类序列化为JSON时,我得到以下结果(这正是我想要的):

{
  "payment" : {
    "creditCardPayment": {
      ...
    } 
}

Unfortunately, if I take the above JSON and try to de-serialise it back into my object model, I get the following exception:

Could not read JSON: Could not resolve type id 'creditCardPayment' into a subtype of [simple type, class Payment] at [Source: org.apache.catalina.connector.CoyoteInputStream@19629355; line: 1, column: 58] (through reference chain: Order["payment"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'creditCardPayment' into a subtype of [simple type, class Payment] at [Source: org.apache.catalina.connector.CoyoteInputStream@19629355; line: 1, column: 58] (through reference chain: Order["payment"])

My application is configured via Spring JavaConf, as follows:

@Configuration
@EnableWebMvc
public class AppWebConf extends WebMvcConfigurerAdapter {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
        return objectMapper;
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper());
        return converter;
    }

    @Bean
    public Jaxb2RootElementHttpMessageConverter jaxbMessageConverter() {
        return new Jaxb2RootElementHttpMessageConverter();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(jaxbMessageConverter());
        converters.add(mappingJacksonMessageConverter());
    }
}

为了进行测试,我有一个带有两个方法的控制器,一个返回HTTP GET请求的命令(这一个有效),另一个通过HTTP POST接受命令(这一个失败),例如.

@Controller
public class TestController {

    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Order getTest() {}

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public void postTest(@RequestBody order) {}

}

I have tried all suggestions from the various discussions on SO but so far had no luck. Can anyone spot what I'm doing wrong?

推荐答案

Try to register subtype using ObjectMapper.registerSubtypes instead of using annotations

Json相关问答推荐

无法根据vega规范中的条件设置文本 colored颜色

使用自定义类型在Golang中解析JSON数组

重构JOLT代码以获得预期输出

Jolt Spec 跳过数组中的第一个元素

如何在 terraform 输出中打印一组用户信息

Golang 解组行为:字段过多?

Vue 3如何将参数作为json发送到axios get

使用 SwiftUI 在 API 调用中解码嵌套 JSON 响应时遇到问题

在 rust 中从 API 反序列化 serde_json

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

JSON Schema 与 XML Schema 的比较及其future

Laravel5 Json 获取文件内容

Android 上的 JSON - 序列化

如何在返回对象的 Spring MVC @RestController @ResponseBody 类中响应 HTTP 状态代码?

编写 JSON 日志(log)文件的格式?

在 Rails 中使用 JSON 创建嵌套对象

使用 API 搜索维基百科

为不同类型的项目数组正确的 JSON Schema

MySQL Select JSON 字段属性具有值的位置

如何对 jq 中的 map 数组中的值求和?