Java Spring rest Java.lang.ClassCastException:Java.util.LinkedHashMap不能强制转换为Cuestionario
我使用SpringREST开发了以下代码 我的代理Java Spring rest Java.lang.ClassCastException:Java.util.LinkedHashMap不能强制转换为Cuestionario,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我使用SpringREST开发了以下代码 我的代理 public Respuesta reject(Integer id,Integer uid, Integer asociation, String reason,Integer type, Cuestionario cuestionario){ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(METHOD_REJECT_FULLPATH); Map
public Respuesta reject(Integer id,Integer uid, Integer asociation, String reason,Integer type, Cuestionario cuestionario){
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(METHOD_REJECT_FULLPATH);
Map<String,Serializable> map = new HashMap<String,Serializable>(2);
map.put("cuestionario",cuestionario);
map.put("motivo",motivo);
ResponseEntity<RespuestaServidor> respuesta = restTemplate.exchange(builder.buildAndExpand(id,uid,type, idTipo,asociation).encode().toUri(),
HttpMethod.POST,
new HttpEntity<Map<String,Serializable>>(map),
new ParameterizedTypeReference<Respuesta>(){});
return respuesta.getBody();
}
下面给出的是提示
public class Cuestionario implements Serializable {
private static final long serialVersionUID = -2669540728368524646L;
private String respuesta1;
private String respuesta2;
private String respuesta3;
private String respuesta4;
private boolean fullfilled;
为此,我得到以下异常
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.Cuestionario
我在与本期类似的其他帖子中读到过,其中指出这是一种Java怪癖,而其他观点则指出可以使用映射器检索/投射Cuestionario对象。但由于我在REST方面的专业知识有限,我不知道有任何制图员
我有一个非常相似的代码,但是有一个
Map<List<Integer>,List<Integer>>
Map
而且效果很好。但这对奎斯蒂奥纳里奥来说是失败的。我也尝试过使用SpringLinkedMultiValueMap,但也不起作用
MultiValueMap<String,Serializable> map = new LinkedMultiValueMap<String, Serializable>(2);
map.add("motivo",motivo);
map.add("cuestionario",cuestionario);
MultiValueMap=newlinkedMultivaluemap(2);
地图。添加(“motivo”,motivo);
添加(“cuestionario”,cuestionario);
我需要在某个地方解析请求主体并从数据中构造一个Cuestionario实例,但我如何才能做到这一点。您试图将一个
对象
强制转换为Cuestionario
,这是行不通的
@RequestMapping(value = Proxy.METHOD_REJECT,method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<Respuesta>reject(@PathVariable Integer id,@PathVariable Integer uid,@PathVariable Integer asociation, @PathVariable Integer type,@RequestBody(required=false)Map<String,Object>map){
final String motivo = (String)map.get("motivo");
// Here map is of type Map<String, Object>
final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario");
然后注释你的Cuestionario
如下:
@JsonDeserialize(using = YourCuestionarioDeserializer.class)
public class Cuestionario
或者做同样的事情,但反序列化到DTO,然后使用DTO创建提示音您是否尝试过
(提示音)(map.get(“提示音”)代码>?(Cuestionario)(map.get(“Cuestionario”);与final Cuestionario Cuestionario=(Cuestionario)map.get(“Cuestionario”)的区别是什么;在第二个场景中,您尝试将地图投射到Cuestionario它不起作用。stackoverflow上有许多帖子看起来很相似,您尝试过google吗?你在试着把一个物体扔到球杆上!是的。但这确实是一种暗示。将其转换为LinkedHashMap。。。我需要获取cuestionario对象。强制转换只会提升继承层次结构。您可以将Cuestionario强制转换为对象,因为Cuestionario继承自对象(就像所有其他Java类一样),不可能实现相反的转换。即使该对象始终是一个提示,JVM也无法知道这一点。如果需要从请求正文中提取Cuestionario,则必须在某个位置解析请求正文,并从数据中构造一个Cuestionario实例,以及如何做到这一点??我添加了一个编辑,其中包含使用Jackson进行反序列化的一般步骤。如果你需要更具体的东西,有很多很好的教程
@RequestMapping(value = Proxy.METHOD_REJECT,method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<Respuesta>reject(@PathVariable Integer id,@PathVariable Integer uid,@PathVariable Integer asociation, @PathVariable Integer type,@RequestBody(required=false)Map<String,Object>map){
final String motivo = (String)map.get("motivo");
// Here map is of type Map<String, Object>
final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario");
public class CuestionarioDeserializer extends JsonDeserializer<Cuestionario> {
@Override
public Cuestionario deserialize(JsonParser parser, DeserializationContext context) throws IOExcention {
//Your deserialization logic
}
}
@JsonDeserialize(using = YourCuestionarioDeserializer.class)
public class Cuestionario