Issue
Hello i met an issue when testing my jersey web service with junit. I tried to add a value in my param
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response parsingPdfOrange(@FormDataParam("fichier") InputStream uploadedInputStream,
@FormDataParam("fichier") FormDataContentDisposition fileDetail,
@FormDataParam("TypeFacture") String typeFile) {
try {
// my code //
return Response.status(201).entity(info).build();
}catch Exception e{
return Response.status(406).entity("pb").build();
}
but i have no idea to add my typeFile into my entity, i've tried this but the string isn't communicate and return a nullpointerexeption
@Test
public void parsePDF_TemplateNotExist() throws Exception {
FileDataBodyPart filePart = new FileDataBodyPart("fichier", new File(currentpath+FactureValide));
BodyPart filePart2 = new BodyPart(templateName, MediaType.TEXT_PLAIN_TYPE);
FormDataMultiPart formDataMultipart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultipart.bodyPart(filePart).bodyPart(filePart2);
Response response = (target("pdf").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA)));
System.out.println(currentpath);
response.close();
formDataMultipart.close();
}
i wish to know how i can resolve my pb and communicate the 2 entities for the test
Edit: this is my jsp <%@ page pageEncoding="UTF-8" %>
<html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta content="utf-8" http-equiv="encoding" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<head>
<meta charset="utf-8" />
<title>Envoi de fichier</title>
<link type="text/css" rel="stylesheet"/>
</head>
<body>
<form action="<%= request.getContextPath() %>/rest/pdf" method="POST" enctype="multipart/form-data" id="fileUploadForm">
<fieldset>
<legend>Envoi de fichier</legend>
<SELECT name="TypeFacture" id=typefacture>
<option>Facture Orange</option>
<option>Facture EDF</option>
</SELECT>
<label for="fichier">Emplacement du fichier <span class="requis">*</span></label>
<input type="file" id="fichier" name="fichier" accept=".pdf" />
<br />
<input type="submit" value="Envoyer" id="btn_envoyer"/>
<br />
</fieldset>
</form>
</body>
Solution
If you just use BodyPart
, you don't get the Content-Disposition
header, which has the name of the body part. BodyPart
is more a more generic multipart. What you want is specifically multipart form-data. For that you can use FormDataBodyPart
. It has the following constructor
FormDataBodyPart(String name, Object entity, MediaType mediaType)
Here you can specify the name or the body part, and this name is what's use to extract it with your @FormDataParam
annotation. So you can use
FormDataBodyPart filePart2
= new FormDataBodyPart("TypeFacture", templateName, MediaType.TEXT_PLAIN_TYPE);
Instead of creating a FormDataBodyPart
, you can also just use the field
method of the FormDataMultiPart
. So you could also just do
FormDataMultiPart formDataMultipart = new FormDataMultiPart()
.field("TypeFacture", templateName, MediaType.TEXT_PLAIN_TYPE);
Or even easier, if it is just a plan text part, you can leave of the MediaType
and it will automatically set it to text/plain
FormDataMultiPart multipart = (FormDataMultiPart) new FormDataMultiPart()
.field("TypeFacture", templateName)
.bodyPart(new FileDataBodyPart("fichier", new File("test.txt")));
Answered By - Paul Samsotha