我正在使用杰克逊图书馆,并试图实现这一点,here
BaseOperationRequest.java
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.social.connect.web.ProviderSignInController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
ZAddBaseOperationRequest.java
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "command"
)
@JsonSubTypes({
@JsonSubTypes.Type(name = "ZADD", value = ZAddBaseOperationRequest.class)
})
public class BaseOperationRequest {
public short operationId;
public Command command;
public String gameId;
public String key;
}
Command.java
public class ZAddBaseOperationRequest extends BaseOperationRequest{
public Map<String, Double> members;
}
当我尝试从REST调用传递Object时,问题出在这里:
public enum Command{
ZADD,
HSET
}
现在,我将其称为:
第一种情况 呼叫没有会员地图:
@RestController
public class MyController{
//keeping just for now as GET, will change it to POST and take it in RequesBody later on
@RequestMapping(value = "/process/{object}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody ResponseEntity process(@Pathvariable String object){
System.out.println(object);//I am getting this as correct--->(A)
BaseOperationRequest[] baseOperationRequestArray = new ObjectMapper().readValue(object, BaseOperationRequest[].class);//getting exception --->(B)
System.out.println(baseOperationRequestArray);
}
}
处理方法正在调用,因为在命令中获取 ZADD 时,Jackson被告知要创建 ZAddBaseOperationRequest 对象,它正在执行但命令本身的值在结果对象中被指定为 null 。
请解释原因? 命令的价值去了哪里?
第二种情况 呼叫会员地图: :/.../过程/ [{ “成员”:{ “一”:1.0}, “operationId”:1, “命令”: “ZADD”, “游戏ID”: “T5”, “关键”:“ABC “}]
然后在这种情况下,等式(A)显示 [{“成员”:{“a”:1.0,b 就是这样,GET的其他部分去了哪里。< / p>
这让我很生气。 :)。 提前谢谢..
请帮忙。
答案 0 :(得分:2)
将json作为路径参数发送不是一个好习惯。
要解决您的问题,请在visible=true
注释中添加JsonTypeInfo
。您的声明将成为:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "command",
visible = true
)
根据visible
的杰克逊文档:
定义类型标识符值是否将作为JSON流的一部分传递给反序列化器(true),或由TypeDeserializer(false)处理和删除的属性。 默认值为false,这意味着Jackson处理并从传递给JsonDeserializer的JSON内容中删除类型标识符。