【发布时间】:2018-04-11 13:48:25
【问题描述】:
我正在使用 Apache HttpConnection 将带有 PUT 请求的文本文件发送到 Swagger 上的服务器主机。似乎我遗漏了一些东西,因为作为响应我得到“内部服务器错误”。 客户端代码:
String url = "http://xxx.xxx.xx.xx:XXXX/warehouse-ui/api/v2/external-file/IMPORT_RATIO?businessUnit=MBA&sourceSystem=AVALOQ_MBA&effectiveFrom=2011-12-21&effectiveTo=MAX&threshold=WARNING";
String encoding = Base64.getEncoder().encodeToString((usrPassPair).getBytes());
InputStream is = new FileInputStream(myLocalFile);
HttpPut request = new HttpPut(url);
HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("test", is, ContentType.create("multipart/form-data"), "asd.txt").build();
request.setHeader("Authorization", "Basic " + encoding);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode()
服务器站点上的代码(Swagger):
@Path ("/{descriptor}")
@PUT
@Consumes ({MediaType.MULTIPART_FORM_DATA})
@ApiOperation (position = 1, value = "Uploads a file and transfers data according to given descriptor and for the specified effective date. Transfer is done synchronously.")
@Produces ("application/json")
public Response uploadFile (
@QueryParam ("businessUnit") @ApiParam(required = true) String businessUnitName,
@QueryParam ("sourceSystem") @ApiParam (required = true, value = "The source system") String sourceSystemName,
@PathParam ("descriptor") @ApiParam (required = true, value = "The descriptor for the file") String descriptor,
@QueryParam ("effectiveFrom") @ApiParam (required = true, value = "The date in ISO format. E.g. 2011-12-21") String effectiveFromString,
@QueryParam ("effectiveTo") @ApiParam (value = "The date in ISO format or 'MAX'. E.g. 2011-12-21", defaultValue = "MAX") String effectiveToString,
@ApiParam (value = "file to upload", required = true) @FormDataParam ("file") InputStream inputStream,
@ApiParam (value = "file detail", required = true) @FormDataParam ("file") FormDataContentDisposition fileDetail,
@QueryParam ("threshold") @ApiParam(required = true, value = "Logging threshold", allowableValues = "MESSAGE,NOTICE,WARNING,ERROR", defaultValue = "WARNING") Severity threshold)
throws IOException, Persistence.Exception, BusinessException, ConfigurationException {
ExternalFile externalFile = new VirtualExternalFile (fileDetail.getFileName (), ByteStreams.toByteArray(inputStream));
TaskExecutionJob<ExternalFile, DataLoadProcessResult> taskExecutionJob = prepareTransfer (businessUnitName, sourceSystemName, descriptor,
EndPointHelper.getEffectivePeriodFrom (effectiveFromString, effectiveToString), externalFile, threshold);
return EndPointHelper.createResponseFor(taskExecutionJob.executeSynchronous ());
是否有我未包含的参数导致此错误?
【问题讨论】:
标签: java rest swagger httpurlconnection apache-httpclient-4.x