Search code examples
javaweb-servicesjax-rsjax-ws

How can I "fake" a file to a web service using Java WS/RS annotations?


I'm trying to post to a webservice. I'm using an annotated interface and then a CXF jaxrs:client configured in Spring. The service expects username, password and a file so this works:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
String uploadFile(
    @QueryParam("username") String username,
    @QueryParam("password") String password,
    @Multipart() File file
);

However sometimes I want to use the service but I don't have the file as a file on disk, only as a string, and I can't write a temporary file. So I'm looking for annotations which will produce the same exact post but from a string. This doesn't work:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
String uploadFile(
    @QueryParam("username") String username,
    @QueryParam("password") String password,
    @Multipart() String file
);

Is there a correct way I can do this with the annotations?


Solution

  • The @Multipart File feature is a convenience. You don't have to use it. You can operate at the next level down. For one thing, it probably works on in InputStream. See the documentation, particularly under 'Uploading files with Client API'.