[英]How to deserialize json to java object, when you expose interface RequestBody in rest call?
i have following project structure as maven project : 我有以下项目结构作为Maven项目:
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) 项目2-> Impl-part(已实现)
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) 项目3->休息部分(有休息电话)
@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. 核心部分JAR包含在impl-part中,而impl-part JAR包含在Rest-part中,并且我部署Rest-part WAR文件。
I am returning the same object in the rest call, but it gives error when this function is called. 我在rest调用中返回相同的对象,但是在调用此函数时会出错。 i tried registering deserializer for the Foo interface via this code : 我试图通过以下代码为Foo接口注册反序列化器:
@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 : FooDeserializer如下:
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. 这段代码无法解决我的问题,并且在将json转换为对象时引发错误。 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
. 首先,它是@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: 从以下位置更改JSON:
{
"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
: 将Demo
更改为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. 根据聊天中的讨论,问题在于存在不同的Demo
实现。 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.