简体   繁体   中英

How to deserialize json to java object, when you expose interface RequestBody in rest call?

i have following project structure as maven project :

Project 1 -> Core-part(having interface) :

interface Foo{

  public String getStr1();
  public setStr1(String str1);

  public List<? extends Bar> getBarList();
  public setBarList(List<? extends Bar> barList);
}

interface Bar{
   public String getStr2();
   public setStr2(String str2);
}

Project 2 -> Impl-part(having implementation)

Public class FooImpl implements Foo{
  private String str1;
  private List<? extends Bar> barList;

  public String getStr1(){
      return str1;
  }
  public setStr1(String str1){
    this.str1= str1;
  }
  public List<? extends Bar> getBarList(){
    return barList;
  }
  public setBarList(List<? extends Bar> barList){
    this.barList= barList;
  }

}

Public class BarImpl implements Bar{
  private String str2;

  public String getStr2(){
      return str2;
  }
  public setStr2(String str2){
    this.str2= str2;
  }
}

Project 3 -> Rest-part (having rest calls)

@RestController
public class BaseDataController {

  @RequestMapping(method=RequestMethod.POST,value="/save")
  public Foo saveFoo(@RequestBody Foo foo) {
        return foo;

  }
}

core-part JAR is included in impl-part and impl-part JAR is included in Rest-part, and i deploy Rest-part WAR file.

I am returning the same object in the rest call, but it gives error when this function is called. i tried registering deserializer for the Foo interface via this code :

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
  @Bean
  @Primary
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new  MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();

    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addDeserializer(Demo.class, new FooDeserializer());
    simpleModule.addDeserializer(Demo.class, new BarDeserializer());
    objectMapper.registerModule(simpleModule);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jsonConverter.setObjectMapper(objectMapper);
    return jsonConverter;
  }

FooDeserializer is as follows :

public class FooDeserializer extends JsonDeserializer<Foo> {

  @Override
  public Foo deserialize(JsonParser jp, DeserializationContext    context) throws IOException {
       ObjectMapper mapper = (ObjectMapper) jp.getCodec();
       ObjectNode root = mapper.readTree(jp);
       return mapper.readValue(root.toString(),  FooImpl.class);
  }
}

public class BarDeserializer extends JsonDeserializer<Bar> {

  @Override
  public Bar deserialize(JsonParser jp, DeserializationContext    context) throws IOException {
       ObjectMapper mapper = (ObjectMapper) jp.getCodec();
       ObjectNode root = mapper.readTree(jp);
       return mapper.readValue(root.toString(),  BarImpl.class);
  }
}

This code doesn't solve my problem and throws error for conversion of json to object. What should i do to solve this issue.

And if i have scenario of interface as member of another interface, how this will be solved. As i tries registering this deserializer too in rest layer project.

The request i post for this:

POST /save HTTP/1.1
Host: XXX
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: XXX

{  
   "str1":"aaa",
   "barList":[  
  {  
     "str2":"bbb"
  }
  ]
}

i have just structured this details minimally, in actual data will increase.

The error which i get is :

abstract types either need to be mapped to concrete types, have custom  deserializer, or contain additional type information\n at [Source:   java.io.PushbackInputStream@32e11e31; line: 6, column: 7]

First of all, it's @RestController . Then, you're missing an annotation:

@RestController
public class BaseDataController {

  @RequestMapping(method=RequestMethod.POST,value="/save")
  public Demo saveDemo(@RequestBody DemoImpl demo) {
        return demo;

  }
}

Change your JSON from:

{
    "Demo":"aaa" 
}

to

{
    "demo":"aaa" 
}

Your request should look as it follows:

POST /save HTTP/1.1
Host: XXX
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: XXX

{  
   "demo":"aaa"
}

You have to pass exactly the implementation which is expected. Change Demo into DemoImpl :

 @RestController
public class BaseDataController {

  @RequestMapping(method=RequestMethod.POST,value="/save")
  public DemoImpl saveDemo(@RequestBody DemoImpl demo) {
        return demo;

  }
}

According to the discussion in the chat, the problem is that there are different Demo implementations. The possible solution might be:

 @RestController
public class BaseDataController {

  @RequestMapping(method=RequestMethod.POST,value="/save/impl1")
  public DemoImpl1 saveDemo(@RequestBody DemoImpl1 demo) {
        return demo;

  }

  @RequestMapping(method=RequestMethod.POST,value="/save/impl2")
  public DemoImpl2 saveDemo(@RequestBody DemoImpl2 demo) {
        return demo;

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM