Java Spring rest java.lang.ClassCastException:java.util.LinkedHashMap无法强制转换为Cuestionario

时间:2018-01-25 14:09:18

标签: java spring rest spring-mvc

我使用Spring Rest开发了以下代码。

MYPROXY

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();
}

URI调用此方法。

@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");
    final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario");        

问题出在这一行

    final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario");        

以下是Cuestionario POJO

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怪癖,而其他意见声明可以使用mapper来检索/转换Cuestionario对象。但由于我对REST的专业知识较少,我不知道有任何地图制作者。

我希望有人可以帮助我。我有一个非常类似的代码,但有一个

Map<List<Integer>,List<Integer>>

工作完美。但Cuestionario失败了。我也尝试过使用Spring LinkedMultiValueMap,但这也不起作用。

MultiValueMap<String,Serializable> map = new LinkedMultiValueMap<String, Serializable>(2);
map.add("motivo",motivo);
map.add("cuestionario",cuestionario);

我需要在某处解析请求主体并从数据构造Cuestionario实例,但我该怎么做。

1 个答案:

答案 0 :(得分:1)

您正试图将Object投射到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"); 

您必须手动反序列化响应正文。

由于你正在使用Spring,你可能已经在你的依赖项中拥有了Jackson。您可以延长JsonDeserializer

public class CuestionarioDeserializer extends JsonDeserializer<Cuestionario> {
  @Override
  public Cuestionario deserialize(JsonParser parser, DeserializationContext context) throws IOExcention {
    //Your deserialization logic
  }
}

然后注释您的Cuestionario,如:

@JsonDeserialize(using = YourCuestionarioDeserializer.class)
public class Cuestionario

或做同样的事情,但反序列化为DTO,然后使用DTO创建Cuestionario

相关问题