Java 如何设置rest模板的动态返回类型
我只想设置Java 如何设置rest模板的动态返回类型,java,rest,object,reflection,Java,Rest,Object,Reflection,我只想设置postForObject返回的对象的动态返回类型。这是我尝试过的,但不起作用。此处的DecryptedData是返回类型,但我将其更改为Object Object[] decryptedDataArray = restTemplate.postForObject(url, dataToBeDecryptedList.toArray(), Object[].class); if(decryptedDataArray instanceof DecryptedData[]){ i
postForObject
返回的对象的动态返回类型。这是我尝试过的,但不起作用。此处的DecryptedData
是返回类型,但我将其更改为Object
Object[] decryptedDataArray = restTemplate.postForObject(url, dataToBeDecryptedList.toArray(), Object[].class);
if(decryptedDataArray instanceof DecryptedData[]){
if (decryptedDataArray != null && decryptedDataArray.length > 0) {
fieldDataMap = setDecryptedData(fieldDataMap, (DecryptedData[]) decryptedDataArray);
}
}
在您的示例中,
restemplate
为什么会返回一个DecryptedData[]
?是的,因为它注定要返回DecryptedData
。但是假设我不知道它要返回什么对象。那么我将如何处理?请解释为什么您认为它注定要返回DecryptedData
。restemplate
知道关于DecryptedData
的什么?这将把数据发布到所需的客户端。客户端将呈现并发送DecryptedData
作为响应。但是如果我不知道客户端响应是什么呢?不。它不会发送DecryptedData
。它将发送一些字节,这些字节将被解释为JSON文本。如果您不让restemplate
进一步将JSON文本解释为JavaDecryptedData[]
对象,它就无法自己猜出它。