2

So I was trying to do a POST using REST with Postman in Chrome, but after I hit send got error

HTTP Status 415 - Unsupported Media Type

My code and part of the screen shot is included, the pair for the hashmap I tried is duration and 150. I am sure the URL is correct but don't know why the media type is not accepted.

@Path("activities")
public class ActivityResource {

    @POST
    @Path("activity")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Activity createActivityParams(MultivaluedHashMap<String,String> formParams){
        return null;
    }
}

enter image description here

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
OPK
  • 4,120
  • 6
  • 36
  • 66
  • I know your name is HashMap, so you probably love HashMaps, but have you tried MultivaluedMap? I haven't tested but it's possible the reader just accepts the super type and not subtypes in it's isAcceptable method. This is just a guess. Don't have my toolbox open right now :-) – Paul Samsotha May 13 '15 at 03:37

1 Answers1

2

Yeah so as I suspected, the FormMultivaluedMapProvider (which handles MultivaluedMap reading for application/x-www-form-urlencoded Content-Type only allows for MultivaluedMap<String, String> or MultivaluedMap, not MultivaluedHashMap, as you have.

Here is the source for the isReadable (which is called when the runtime looks for a MessageBodyReader to handle the combination of Java/Content-Type types.

@Override
public boolean isReadable(Class<?> type, Type genericType, 
                          Annotation[] annotations, MediaType mediaType) {
    // Only allow types MultivaluedMap<String, String> and MultivaluedMap.
    return type == MultivaluedMap.class
            && (type == genericType || mapType.equals(genericType));
}

As as side note, on the writing side, it's a different story, you can see the isWriteable method, uses isAssignableFrom, which if the isReadable had, you would be able to use the MultivaluedHashMap as your method parameter.

@Override
public boolean isWriteable(Class<?> type, Type genericType, 
                           Annotation[] annotations, MediaType mediaType) {
    return MultivaluedMap.class.isAssignableFrom(type);
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720