I have developed the following code using 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();
}
The URI calls this method.
@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");
The problem is in this line
final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario");
Given below is the 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;
for which I am getting the following exception
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.Cuestionario
I have read in other posts similar to this issue which state that it is a kind of Java quirk while other opinions state that a mapper can be used to retreive/cast the Cuestionario object. But I am not aware of any mappers due to my limited expertise on REST.
I have a very similiar code but with a
Map<List<Integer>,List<Integer>>
And works perfect. But it is failing for Cuestionario. I have also tried using Spring LinkedMultiValueMap but that doesn't work either.
MultiValueMap<String,Serializable> map = new LinkedMultiValueMap<String, Serializable>(2);
map.add("motivo",motivo);
map.add("cuestionario",cuestionario);
I need to have to parse the request body somewhere and construct a Cuestionario instance from the data but how can i do that.
(Cuestionario)(map.get("cuestionario"));?