Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如果可以更改退货类型';不能在rest模板中反序列化吗?_Json_Spring_Resttemplate_Json Deserialization - Fatal编程技术网

Json 如果可以更改退货类型';不能在rest模板中反序列化吗?

Json 如果可以更改退货类型';不能在rest模板中反序列化吗?,json,spring,resttemplate,json-deserialization,Json,Spring,Resttemplate,Json Deserialization,我调用restTemplate: restTemplate.exchange(uri, HttpMethod.GET, prepareHttpEntity(), MyDeserializedClass.class); MyDeserializedClass: public class MyDeserializedClass { private final String id; private final String title; @JsonCreator

我调用restTemplate:

restTemplate.exchange(uri, HttpMethod.GET, prepareHttpEntity(), MyDeserializedClass.class);
MyDeserializedClass:

public class MyDeserializedClass {

    private final String id;
    private final String title;

    @JsonCreator
    public MyDeserializedClass(@JsonProperty("id") String id,
                    @JsonProperty("title") String title) {
        this.pageId = pageId;
        this.title = title;
    }
}
当json中没有对象时,我将获得
MyDeserializedClass
null

我试图用
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
但运气不佳


在这种情况下,有没有办法检索另一个对象(或某种回调?

您可以自己尝试反序列化对象,即:

ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, prepareHttpEntity(), String.class);
try {
   MyDeserializedClass myClass = new ObjectMapper().readValue(response.body, MyDeserialized.class);
   return ResponseEntity.ok(myClass);
} catch(IOException e) {
   //log exception
   return ResponseEntity.notFound().build();
}
ResponseEntity response=restemplate.exchange(uri,HttpMethod.GET,prepareHttpEntity(),String.class);
试一试{
MyDeserializedClass myClass=new ObjectMapper().readValue(response.body,MyDeserialized.class);
返回ResponseEntity.ok(myClass);
}捕获(IOE异常){
//日志异常
返回ResponseEntity.notFound().build();
}

您可以使用静态函数作为主
@JsonCreator
而不是构造函数

public class MyDeserializedClass {

    private final String id;
    private final String title;

    public MyDeserializedClass () {}

    @JsonCreator
    public static MyDeserializedClass JsonCreator(@JsonProperty("id") String id, @JsonProperty("title") String title){
        if (id == null || title == null) return null;
        //or some other code can go here

        MyDeserializedClass myclass = new MyDeserializedClass();

        myclass.id = id; // or use setters
        myclass.title = title;

        return myclass;
    }
}

通过这种方式,您可以返回
null
或某种MyDeserializedClass子类,而不是带有null值的
MyDeserializedClass

null
或某种MyDeserializedClass子类,而不是带有null值的
MyDeserializedClass
修复它?为什么不返回字符串并使用jackson地图?如果它是空的,则很容易检测。@varren如果我得到的是空的,而不是具有空值的对象,那么它将满足我的要求