【问题标题】:RestEasy - UnsupportedMediaTypeException: Cannot consume content typeRestEasy - UnsupportedMediaTypeException:无法使用内容类型
【发布时间】:2012-03-09 17:56:13
【问题描述】:

我有一个 Spring MVC REST 服务,它应该接收混合的 mime 内容。控制器定义为

    @POST
    @Path("/createMime")
    @Consumes("multipart/mixed")
    @ResponseStatus(HttpStatus.OK)
    public String createMime(@Context ServletContext servletContext, MultipartInput input) throws MyRestException {

        logger.info("Processing /createMime");

        return "TEST";
    }

当我测试上述内容时,我收到以下错误:

17:18:58,456 WARN  [org.jboss.resteasy.core.SynchronousDispatcher] (http--127.0.0.1-8080-1) Failed executing POST services/createMime: org.jboss.resteasy.spi.UnsupportedMediaTypeException: Cannot consume content type
        at org.jboss.resteasy.core.registry.Segment.match(Segment.java:112) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:251) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:173) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:118) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.2.1.GA.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.2.1.GA.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.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
        at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.2.Final.jar:7.0.2.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
        at java.lang.Thread.run(Thread.java:619) [:1.6.0_07]

如果我更改@Consumes 注释,使其使用@Consumes("multipart/form-data") 而不是@Consumes("multipart/mixed"),那么它可以工作。我需要做什么才能让它与 multipart/mixed 一起使用?

这是我使用客户端将内容发送到 REST 服务的方式。

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost:8080/MyRestService/rest/services/createMime");

        Scanner scanner = 
           new Scanner(new File("header.xml")).useDelimiter("\\Z");
           String messageHeader = scanner.next();
           scanner.close(); 

        FileBody bin = new FileBody(new File("myImage.jpg"));
        StringBody header = new StringBody(messageHeader.toString());

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("header", header);
        reqEntity.addPart("payload", bin);
        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);

我已经为此苦苦挣扎了很长一段时间,因此我们将不胜感激任何帮助。

谢谢

【问题讨论】:

  • 您是否意识到您的客户实际上发送了multipart/form-data 请求?问题出在客户端。
  • @BalusC - 这很有趣。你怎么知道它是,我如何发送多部分/混合请求?
  • 另请注意,您不能使用multipart/mixed 上传文件内容。
  • 这是 Spring MVC 还是 Jersey?您在看起来像传统 Spring MVC 控制器的地方使用 Jersey 注释。

标签: java spring rest resteasy multipart


【解决方案1】:

您是否检查了请求的内容类型?我相信您将内容发送到REST 服务的客户正在发布multipart/form-data 请求。

要了解如何发送multipart/mixed request,您应该查看http://www.w3.org/Protocols/rfc1341/7_2_Multipart.htmlmultipart 内容类型的文档,第7.2.1 节建议应指定并使用boundary

基本上,您需要按照以下几点发出multipart/mixed 请求。

在标题中包含边界

Content-Type: multipart/mixed;boundary=gc0p4Jq0M2Yt08jU534c0p

请求中的每个部分都应该有--boundary

 --gc0p4Jq0M2Yt08jU534c0p

最后一部分应该是--boundary--

--gc0p4Jq0M2Yt08jU534c0p--

【讨论】:

    【解决方案2】:

    尝试使用 javax.ws.rs.core.MediaType 代替输入字符串。它避免了可能的错别字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2011-09-19
      • 2011-03-06
      • 1970-01-01
      • 2021-09-22
      • 2015-08-20
      • 2012-12-31
      相关资源
      最近更新 更多