I´m developing a Jax-Rs service, and I have to create something with and without a file.
@POST
@Path("makesomething")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String makesomething(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
//make something
}
If I don´t send a file I receive an error message, saying that is necessary to send a file. How can I make this without sending a file?
André is right, something like this should work just fine (assuming when you aren't uploading a file you are just posting form data):
@POST
@Path("makesomething")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String makesomething(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
makeSomethingImpl(uploadedInputStream, fileDetail, variable, request);
}
@POST
@Path("makesomething")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public String makesomething(@FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
makeSomethingImpl(null, null, variable, request);
}
private String makeSomethingImpl(final InputStream uploadedInputStream, final FormDataContentDisposition fileDetail, final String variable, final HttpServletRequest request) {
// make something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.