3

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.

5
  • Did u try (Cuestionario)(map.get("cuestionario")); ? Commented Jan 25, 2018 at 14:21
  • (Cuestionario)(map.get("cuestionario")); what is the diff with final Cuestionario cuestionario = (Cuestionario)map.get("cuestionario"); Commented Jan 25, 2018 at 14:27
  • In the second scenario, you're trying to cast map to Cuestionario Commented Jan 25, 2018 at 14:29
  • it doesnt work pal Commented Jan 25, 2018 at 14:38
  • There are many posts on stackoverflow that looks similar, did you try google out? google.com/… Commented Jan 25, 2018 at 17:29

1 Answer 1

1

You're trying to cast an Object to a Cuestionario, that won't work

@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"); 

You'll have to deserialize manually the response body.

Since you're using Spring, you probably already have Jackson in your dependencies. You can extend JsonDeserializer

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

and then annotate your Cuestionario like:

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

Or do the same thing but deserialize to a DTO, then use the DTO to create a Cuestionario

Sign up to request clarification or add additional context in comments.

4 Comments

You're trying to cast an Object to a Cuestionario! Yep. But is a Cuestionario indeed. Whose get transform it into a LinkedHashMap... I need to get the cuestionario object.
Casting only works up the inheritance hierarchy. You can cast a Cuestionario to an Object because Cuestionario inherits from Object (just like every other Java class), the inverse is not possible. Even if that Object is always a Cuestionario, the JVM has no way to know that. If you need to extract a Cuestionario from the request body, you have to parse the request body somewhere and construct a Cuestionario instance from the data
and how i can do that??
I added an edit with the general steps for deserializing with Jackson. There are a lot of good tutorials for that if you need something more specific

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.