RestEasy - UnsupportedMediaTypeException: Content Type Cannot Be Used

I have a spring MVC REST service that should receive mixed facial content. The controller is defined as

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

When I test above, I get the following error:

 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] 

If I modify the @Consumes annotation to use @Consumes("multipart/form-data") instead of @Consumes("multipart/mixed") , then it works. What do I need to do to get it working with multipart / mixed?

This is how I use the client to send content to the REST service.

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

I struggled with this for quite some time, so any help would be greatly appreciated.

thanks

+6
source share
2 answers

Have you checked the type of request content? I believe that the client sending the content to the REST service sends a multipart/form-data request.

To understand how to send a multipart/mixed request , you should look at the documentation of the multipart content type in http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html , in section 7.2.1 it is suggested to specify the boundary boundary .

Basically, you need to follow below to make a multipart/mixed request.

Include border in title

 Content-Type: multipart/mixed;boundary=gc0p4Jq0M2Yt08jU534c0p 

Each part of the request must have - boundary

  --gc0p4Jq0M2Yt08jU534c0p 

The last part should consist of - boundary -

 --gc0p4Jq0M2Yt08jU534c0p-- 
+1
source

Try using javax.ws.rs.core.MediaType instead of entering a string. This avoids possible typos.

0
source

Source: https://habr.com/ru/post/908787/


All Articles