我正在使用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 ());
是否有一个我不包括的参数,导致此错误?
答案 0 :(得分:1)
我想如果有人会遇到类似的问题,我会发布我的工作答案,也许更了解REST架构的人可以解释为什么需要这样做:
URIBuilder builder = new URIBuilder()
.setScheme("http")
.setHost("localhost:9090")
.setPath("warehouse-ui/api/v2/external-file/"+context.get("descriptor").toString())
.setParameter("businessUnit", context.get("businessUnit").toString())
.setParameter("sourceSystem", context.get("sourceSystem").toString())
.setParameter("effectiveFrom", context.get("effectiveFrom").toString())
.setParameter("effectiveTo", context.get("effectiveTo").toString())
.setParameter("threshold", context.get("threshold").toString());
String encoding = Base64.getEncoder().encodeToString((usernamePasswordPair).getBytes());
File isF = new File(pathToFile);
FileInputStream fis = null;
fis = new FileInputStream(isF);
HttpPut request = new HttpPut(builder.toString());
HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new InputStreamBody(fis, isF.getName())).build();
request.setHeader("Authorization", "Basic " + encoding);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
System.out.println(code);
System.out.println(response.getStatusLine().getReasonPhrase());
这是从Talend tJava组件粘贴的代码,因此在您的代码中您可能希望处理异常。