を使うポリモーフィックなオブジェクトモデルにJSONをデシリアライズ私は春のMVC(v3.2.0.RELEASE)Webアプリケーションで、次のオブジェクトモデルがあります:私はするためのクラスをシリアライズするとき春とJsonTypeInfo注釈
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 {}
をJSONは、私は(私が望む正確に何である)以下の結果が得られます。
{
"payment" : {
"creditCardPayment": {
...
}
}
残念ながら、私は上記のJSONを取るとしようとする私のオブジェクトモデルに戻し、それをデシリアライズ場合、私は次の例外を取得します:
Could not read JSON: Could not resolve type id 'creditCardPayment' into a subtype of [simple type, class Payment] at [Source: [email protected]; 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: [email protected]; line: 1, column: 58] (through reference chain: Order["payment"])
次のように私のアプリケーションは、スプリングJavaConfを介して構成されている:
@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());
}
}
試験のために、私は2つの方法を有するコントローラは、一方がHTTP GETリクエスト(この1つは動作)と、1つの注文を返す持っています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) {}
}
私は様々な議論からすべての提案を試みましたが、これまで運がなかった。誰でも私が間違っていることを見つけることができますか?
! 'Payment'インターフェースから' @ JsonSubTypes.Type(name = "creditCardPayment"、value = CreditCardPayment.class)を削除し、 'AppWebConf'クラスに' objectMapper.registerSubtypes(CreditCardPayment.class);を追加しました。私の設定を一般的なものにしたいと思っていたので、注釈がうまくいかなかったのは残念ですが、少なくともこの問題は私には分かりません。ありがとうございました。 – grigori
私は同じ問題に直面し、上記の解決策は役に立たない。 –