HTTP状态415 - 执行POST时不支持的媒体类型 [英] HTTP Status 415 - Unsupported Media Type when doing POST
问题描述
所以我试图在Chrome中使用REST与 Postman
进行 POST
,但在我点击发送后错误
So I was trying to do a POST
using REST with Postman
in Chrome, but after I hit send got error
HTTP状态415 - 不支持的媒体类型
HTTP Status 415 - Unsupported Media Type
我的代码和屏幕截图的一部分包含在内,我试过的 hashmap
的对是持续时间
和 150
。我确定网址是正确的,但不知道为什么媒体类型不被接受。
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;
}
}
推荐答案
是的,我怀疑, FormMultivaluedMapProvider
(处理 MultivaluedMap
读取 application / x-www-form-urlencoded
Content-Type仅允许 MultivaluedMap< String,String>
或 MultivaluedMap
,而不是 MultivaluedHashMap
,就像你一样。
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.
这是 isReadable
(当运行时查找 MessageBodyReader
以处理组合时调用Java / Content-Type类型。
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));
}
如旁注,在写作方面,这是一个不同的故事,你可以看到 isWriteable
方法,使用 isAssignableFrom
,如果 isReadable
,则可以使用 MultivaluedHashMap
作为方法参数。
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);
}
这篇关于HTTP状态415 - 执行POST时不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!