如果客户端连接到这样的Web服务(JAX-RS):
URL u = new URL(server);
URLConnection con = u.openConnection();
con.setDoOutput(true);
con.getOutputStream().write(stream.toByteArray()); // ByteArrayOutputStream
con.connect();
InputStream inputStream = con.getInputStream();
byte [] urlBytes = new byte [inputStream.available()];
inputStream.read(urlBytes);
url = new String(urlBytes);
那么Web服务接口定义应该是什么?我有:
@POST
@Path("/upload")
@Consumes("image/jpeg")
@Produces(MediaType.TEXT_PLAIN)
String uploadPicture();
但是,当客户端访问时,它会抛出:
[ERROR] INFO:生成jax-rs代理加载器类。 [INFO] DEBUG [SynchronousDispatcher] PathInfo:/ blob / upload [INFO] WARN [ExceptionHandler]执行POST / blob / upload失败[INFO] org.jboss.resteasy.spi.UnsupportedMediaTypeException:无法使用 内容类型[INFO] at org.jboss.resteasy.core.registry.Segment.match(Segment.java:117) [INFO] at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33) [INFO] at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [INFO] at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44) [INFO] at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327) [INFO] at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [INFO] at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [INFO] at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:354) [INFO] at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:231) [INFO] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:170) [INFO] at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [INFO] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [INFO] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [INFO] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)[INFO] 在 org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166) [INFO] at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:66) [INFO] at com.googlecode.objectify.cache.AsyncCacheFilter.doFilter(AsyncCacheFilter.java:39) [INFO] at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) [INFO] at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) [INFO] at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [INFO] at org.apache.shiro.web.servlet.AbstractShiroFilter $ 1.call(AbstractShiroFilter.java:365) [INFO] at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [INFO] at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [INFO] at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) [INFO] at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [INFO] at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [INFO] at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) [INFO] at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) [INFO] at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) [INFO] at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at com.google.appengine.tools.development.DevAppServerServersFilter.doDirectRequest(DevAppServerServersFilter.java:369) [INFO] at com.google.appengine.tools.development.DevAppServerServersFilter.doDirectServerRequest(DevAppServerServersFilter.java:352) [INFO] at com.google.appengine.tools.development.DevAppServerServersFilter.doFilter(DevAppServerServersFilter.java:115) [INFO] at org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) [INFO] at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) [INFO] at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) [INFO] at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) [INFO] at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) [INFO] at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) [INFO] at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97) [INFO] at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) [INFO] at com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:438) [INFO] at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) [INFO]在org.mortbay.jetty.Server.handle(Server.java:326)[INFO] at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) [INFO] at org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:938) [INFO]在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) [INFO] at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) [INFO] at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) [INFO] at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) [INFO] at org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)
此客户端的JAX-RS接口应该是什么?
更新
这里需要注意的一点是,客户端代码已经编译和签名,我无法改变它。
答案 0 :(得分:5)
太糟糕了,你无法改变客户端...错误属于那里:
未设置相应的Content-Type
,因此连接默认为application/x-www-form-urlencoded
然后它将图像数据作为简单的流发送(因此未正确编码)
可行解决方案的最短(艰难而不那么优雅)的道路可能是接受一切:
@POST
@Path("/upload")
@Consumes("*/*")
@Produces(MediaType.TEXT_PLAIN)
String uploadPicture(InputStream image);