[英]Accessing a micro-service from Java application
I am accessing a microservice from Java and it results in 404-Not found response, while the same things work fine via Swagger-ui. 我正在从Java访问微服务,结果是404(未找到),而通过Swagger-ui可以正常工作。
I am using appache HTTPClient. 我正在使用appache HTTPClient。
I am having the microservice, a simple Spring controller that is to send files via ftp. 我有微服务,一个简单的Spring控制器,可以通过ftp发送文件。 Accepts a json object with fields for host, user password, etc... It works fine, when I test it with the swagger-ui.
接受带有主机,用户密码等字段的json对象。当我使用swagger-ui测试它时,它工作正常。 But, I need to be able to use it programmatically and from the java concole application.
但是,我需要能够以编程方式从Java conconle应用程序中使用它。 The following is a code that I have
以下是我拥有的代码
Controller code 控制器代码
@RequestMapping(value = "/SendFiles", method = RequestMethod.POST, consumes
="application/json", produces = "application/json")
public ResponseEntity sendFiles(@RequestBody FTPObject obj) throws
UnknownHostException {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(obj.getFtpUser(), obj.getFtpHost(),
Integer.parseInt(obj.getFtpPort()) );
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(obj.getFtpPassword());
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(obj.getRemoteDirectory() );
for(FTPFileObject file: obj.getFiles() ) {
sftpChannel.put("C:\\Developer\\projects\\test-
src\\fileSrc\\testUploadSFtpFile_"+i+".txt",
"testUpload_"+i+".txt");
sftpChannel.put(file.getPathFrom()+file.getFromFileName(),
file.getToFileName());
sftpChannel.exit();
session.disconnect();
return new ResponseEntity("Successfully transmitted list of files",
HttpStatus.OK);
}
catch (JSchException e) {
e.printStackTrace();
return new ResponseEntity("Failed to transmit list of files - >
"+e.getMessage(), HttpStatus.EXPECTATION_FAILED);
}
catch (SftpException e) {
e.printStackTrace();
return new ResponseEntity("Failed to transmit list of files - >
"+e.getMessage(), HttpStatus.EXPECTATION_FAILED);
}
}
And this is a fragment of a test 这是测试的一部分
String jsonStr = mapper.writeValueAsString(object);
System.out.println(jsonStr); //This is output I use in swagger-ui
HttpClient httpClient = HttpClientBuilder.create().build(); //Use
// this instead
String postUrl = "http://localhost:8080/WinAPI/sendFiles" ;
HttpPost post = new HttpPost(postUrl);
StringEntity postingString = new StringEntity( jsonStr );
post.setEntity(postingString);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
System.out.println(response.getStatusLine() );
When I run this and use a swagger-ui, I get files transfered to ftp. 当我运行此程序并使用swagger-ui时,我将文件传输到ftp。 But when I run a test java class (2nd code piece) no exceptions or errors, but I get a 404 response.
但是,当我运行测试Java类(第二个代码段)时,没有异常或错误,但是却收到404响应。
Please, help. 请帮忙。 What could be the problem ?
可能是什么问题呢 ?
Endpoints can be case sensitive. 端点可能区分大小写。 Try changing:
尝试更改:
String postUrl = "http://localhost:8080/WinAPI/sendFiles" ;
to 至
String postUrl = "http://localhost:8080/WinAPI/SendFiles" ; // the endpoint described in the controller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.