不使用表单字段的 Quarkus 多部分客户端
Quarkus multipart client without use form field
我想为旧代码编写一个休息客户端,据我所知,它接受多部分。
我的客户端是用 quarkus 编写的,使用的是 resteasy-multipart-provider
我有我想调用的旧代码:
@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
public Response addFiles(@Context HttpServletRequest request, @Context ServletContext context)
{
try
{
File repository = (File) context.getAttribute("javax.servlet.context.tempdir");
DiskFileItemFactory factory = Utils.getDiskFileItemFactory(context, repository);
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List<FileItem> items = upload.parseRequest(request);
for (FileItem item: items)
{
if (!item.isFormField())
{
....
}
}
.....
}
我的客户:
@Path("/upload")
@RegisterRestClient(configKey = "scannedimage")
@ClientHeaderParam(name = "Authorization", value = "{lookupAuth}")
public interface UploadClient extends BearerAuthorizedHeader {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response upload(@MultipartForm UploadBody data);
}
public class UploadBody {
@FormParam("objectId")
@PartType(MediaType.TEXT_PLAIN)
public Long long1;
@FormParam("entityId")
@PartType(MediaType.TEXT_PLAIN)
public Long long2;
@FormParam("doctype")
@PartType(MediaType.TEXT_PLAIN)
public Long documentType;
@FormParam("file")
@PartFilename("{file.getName}")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
public File file;
}
始终响应return空列表
{
"lon1": 1,
"long2": 2,
"list": [],
"error": ""
}
我做错了什么
我有一个使用 org.apache.httpcomponents:httpmime
创建的有效请求示例
HttpEntity entity = MultipartEntityBuilder.create().addTextBody("long1", "1").addTextBody("long2", "2499").addTextBody("doctype", "3306").addBinaryBody("file", file, ContentType.create("application/octet-stream"), "test.pdf").build();
HttpPost httpPost = new HttpPost("http://serviece/upload");
httpPost.setEntity(entity);
httpPost.setHeader("Authorization", "Bearer token");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();
System.out.println(EntityUtils.toString(result));
但如果可能的话,我想用rest-client实现它
结果。我使用了 org.apache.httpcomponents:httpmime:4.5.3 并编写了方法:
public UploadResponse upload(String long1, String long2, String documentType, String fileName, InputStream file) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("long1", long1)
.addTextBody("long2", long2)
.addTextBody("documentType", documentType)
.addBinaryBody("file", file, ContentType.create(MediaType.APPLICATION_OCTET_STREAM), fileName)
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.setCharset(StandardCharsets.UTF_8)
.build();
HttpPost httpPost = new HttpPost(url + "/upload");
httpPost.setEntity(entity);
httpPost.setHeader("Authorization", "Bearer " + token());
HttpResponse response = httpClient.execute(httpPost);
return objectMapper.readValue(response.getEntity().getContent(), UploadResponse.class);
} catch (IOException e) {
log.error("Error create httpClient", e);
}
return new UploadResponse();
}
因为我找不到像通过 resteasy-multipart-provider 添加文件名到文件 FormParam :(。
也许它会修复下一个版本 quarkus 或 restEasy
我想为旧代码编写一个休息客户端,据我所知,它接受多部分。 我的客户端是用 quarkus 编写的,使用的是 resteasy-multipart-provider 我有我想调用的旧代码:
@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
public Response addFiles(@Context HttpServletRequest request, @Context ServletContext context)
{
try
{
File repository = (File) context.getAttribute("javax.servlet.context.tempdir");
DiskFileItemFactory factory = Utils.getDiskFileItemFactory(context, repository);
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List<FileItem> items = upload.parseRequest(request);
for (FileItem item: items)
{
if (!item.isFormField())
{
....
}
}
.....
}
我的客户:
@Path("/upload")
@RegisterRestClient(configKey = "scannedimage")
@ClientHeaderParam(name = "Authorization", value = "{lookupAuth}")
public interface UploadClient extends BearerAuthorizedHeader {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response upload(@MultipartForm UploadBody data);
}
public class UploadBody {
@FormParam("objectId")
@PartType(MediaType.TEXT_PLAIN)
public Long long1;
@FormParam("entityId")
@PartType(MediaType.TEXT_PLAIN)
public Long long2;
@FormParam("doctype")
@PartType(MediaType.TEXT_PLAIN)
public Long documentType;
@FormParam("file")
@PartFilename("{file.getName}")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
public File file;
}
始终响应return空列表
{
"lon1": 1,
"long2": 2,
"list": [],
"error": ""
}
我做错了什么
我有一个使用 org.apache.httpcomponents:httpmime
创建的有效请求示例HttpEntity entity = MultipartEntityBuilder.create().addTextBody("long1", "1").addTextBody("long2", "2499").addTextBody("doctype", "3306").addBinaryBody("file", file, ContentType.create("application/octet-stream"), "test.pdf").build();
HttpPost httpPost = new HttpPost("http://serviece/upload");
httpPost.setEntity(entity);
httpPost.setHeader("Authorization", "Bearer token");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();
System.out.println(EntityUtils.toString(result));
但如果可能的话,我想用rest-client实现它
结果。我使用了 org.apache.httpcomponents:httpmime:4.5.3 并编写了方法:
public UploadResponse upload(String long1, String long2, String documentType, String fileName, InputStream file) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("long1", long1)
.addTextBody("long2", long2)
.addTextBody("documentType", documentType)
.addBinaryBody("file", file, ContentType.create(MediaType.APPLICATION_OCTET_STREAM), fileName)
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.setCharset(StandardCharsets.UTF_8)
.build();
HttpPost httpPost = new HttpPost(url + "/upload");
httpPost.setEntity(entity);
httpPost.setHeader("Authorization", "Bearer " + token());
HttpResponse response = httpClient.execute(httpPost);
return objectMapper.readValue(response.getEntity().getContent(), UploadResponse.class);
} catch (IOException e) {
log.error("Error create httpClient", e);
}
return new UploadResponse();
}
因为我找不到像通过 resteasy-multipart-provider 添加文件名到文件 FormParam :(。 也许它会修复下一个版本 quarkus 或 restEasy