无法使用内容类型 - JAX-RS

xkm*_*xkm 2 java jax-rs resteasy

如果客户端连接到这样的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);
Run Code Online (Sandbox Code Playgroud)

那么Web服务接口定义应该是什么?我有:

@POST 
@Path("/upload")
@Consumes("image/jpeg")
@Produces(MediaType.TEXT_PLAIN)
String uploadPicture();
Run Code Online (Sandbox Code Playgroud)

但是,当客户端访问时,它会抛出:

[错误]信息:生成jax-rs代理加载器类.[INFO] DEBUG [SynchronousDispatcher] PathInfo:/ blob/upload [INFO] WARN [ExceptionHandler]执行POST/blob/upload失败[INFO] org.jboss.resteasy.spi.UnsupportedMediaTypeException:无法在org中使用内容类型[INFO]. jboss.resteasy.core.registry.Segment.match(Segment.java:117)[INFO]位于org.jboss的org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33)[INFO]. resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327)[INFO]位于org.jboss.resteasy的org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44)[INFO]. core.registry.RootSegment.matchChildren(RootSegment.java:327)[INFO] at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:

该客户端的JAX-RS接口应该是什么?

更新:

这里需要注意的一点是客户端代码已经编译和签名,我不能只是改变它.

Car*_*ini 5

太糟糕了,你不能改变客户端...错误属于那里:

  • 没有设置合适的Content-Type,所以连接默认为application/x-www-form-urlencoded

  • 然后它将图像数据作为简单的流发送(因此它没有被正确编码)

通往可行解决方案的最短(艰难而不是那么优雅)的道路可能是接受一切:

@POST 
@Path("/upload")
@Consumes("*/*")
@Produces(MediaType.TEXT_PLAIN)
String uploadPicture(InputStream image);
Run Code Online (Sandbox Code Playgroud)