HTTP 状态 415 - 无法使用内容类型
Posted
技术标签:
【中文标题】HTTP 状态 415 - 无法使用内容类型【英文标题】:HTTP Status 415 - Cannot consume content type 【发布时间】:2015-08-20 01:47:38 【问题描述】:对 REST 服务的 POST 操作返回 JSON 正文
org.jboss.resteasy.spi.UnsupportedMediaTypeException
:不能消费内容类型异常
@Consumes(MediaType.APPLICATION_JSON)
和 @Consumes("application/json")
都返回了相同的异常。
我尝试使用 Postman API 客户端调用该服务。
@RolesAllowed("admin")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/auth")
public Response login(UserCl usr)
if(usr.getUsername().compareTo("user") == 0 &&
usr.getPassword().compareTo("pass") == 0)
return Response.status(200).build();
else
return Response.status(401).build();
12:44:07,322 WARN [org.jboss.resteasy.core.SynchronousDispatcher] (http-localhost-127.0.0.1-8080-1) Failed executing POST user-service/auth: org.jboss.resteasy.spi.UnsupportedMediaTypeException: Cannot consume content type
at org.jboss.resteasy.core.registry.Segment.match(Segment.java:117) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:307) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:173) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:118) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
【问题讨论】:
【参考方案1】:您尚未设置 Content-Type
标头。不记得默认值是什么,但您可以查看 Chrome 开发人员工具以查看带有标头的完整请求。
下拉列表中的 JSON 值仅用于语法高亮。它实际上并没有设置标题。所以只需添加标题
Content-Type ->> application/json
【讨论】:
以上是关于HTTP 状态 415 - 无法使用内容类型的主要内容,如果未能解决你的问题,请参考以下文章
Sonos cancelAudioClip API 返回 415 HTTP 响应
SQL CLR 存储过程调用 Web 服务接收 System.Net.WebException:请求失败,HTTP 状态 415:不支持的媒体类型