CXF" No Message body reader found"用于POST服务中的字节数组参数

时间:2014-11-06 16:09:39

标签: java web-services cxf jax-rs

我正在尝试通过将文件作为 POST 实体中的字节数组来编写一个负责上传文件的服务。这是我的代码

我的CXF服务

@Path("MyTest")
public class TestService {
    @POST
    public String MyPost(Byte[] bytes){
        System.out.println("Service invoked");
        return "Hello, I am a POST response";
    }
}

我的客户

File image = new File("C:\\snake.jpg");
FileInputStream is = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] fileInBytes = bos.toByteArray();

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("MyTest");
Response response = target.request().post(Entity.entity(fileInBytes, MediaType.APPLICATION_OCTET_STREAM));

InputStream i = (InputStream) response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(i));
System.out.println(br.readLine());

这是我得到的错误

SEVERE: No message body reader has been found for class [Ljava.lang.Byte;, ContentType: application/octet-stream
Nov 06, 2014 4:02:50 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1298)
...

有关它的任何想法?有没有更好的方法来进行文件上传服务?

由于

2 个答案:

答案 0 :(得分:1)

您可以使用InputStream

@POST
public String MyPost(InputStream is) throws IOException  {
    BufferedImage image = ImageIO.read(is);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}

除此之外,在您的客户端代码中,您还没有实际向输出流写入任何内容。它应该更像是

FileInputStream is = new FileInputStream(image);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] fileInBytes = buffer.toByteArray();

Response response = target.request().post(Entity.entity(fileInBytes, 
        MediaType.APPLICATION_OCTET_STREAM));

<强>更新

可以实际使用字节数组。您只需使用原语byte[]

@POST
public String MyPost(byte[] bytes) throws IOException  {
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bytes)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}

甚至可以使用File

@POST
public String MyPost(File file) throws IOException  {
    BufferedImage image = ImageIO.read(file);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}

JAX-RS为磁盘上的输入创建临时文件。它从网络缓冲区读取并将读取的字节保存到此临时文件中。

他们都工作。选择你的毒药

答案 1 :(得分:1)

有完全相同的问题。将application / octet-stream作为参数添加到@Consumes注释中解决了问题:

@POST
@Consumes({"application/octet-stream", "text/xml", "application/xml"})
@Produces({"text/xml", "application/xml"})
public Object post(String xmlData) throws Exception {
    ...
}

但是,如果您可以确保调用者在HTTP请求标头中设置适当的Content-Type,则可能没有必要将“application / octet-stream”添加到@Consumes注释中。

使用CURL的示例:

curl -vX POST -H“Content-Type:text / xml”-T“postdata.xml”“http://localhost:9000/ws/rest/ ...”