简体   繁体   English

从Java应用程序访问微服务

[英]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.

相关问题 Java中的微服务中的日志记录和异常处理 - Logging and Exception handling in a micro-service in Java Java中Spring-Boot微服务的计算监控指标 - Computing Monitoring metrics of Spring-Boot Micro-Service in Java 微服务架构中的错误源传播 - Error source propagation in micro-service architecture 微服务实例之间的JPA同步 - JPA synchronization between micro-service instances 如何在 Heroku 上托管 spring-boot 微服务 dockerized(docker compose)应用程序 - How to host spring-boot micro-service dockerized(docker compose) application on Heroku 内部微服务请求在Spring Cloud应用程序中以禁止状态响应 - inter micro-service request responds with Forbidden status in spring cloud application 为多模块微服务 spring 启动应用程序设置 swagger 文档 - Setting up swagger documentation for a multi-module micro-service spring boot application 如何使用尤里卡服务器在 spring 启动应用程序中禁用微服务实例? - how to disable an instance of micro-service in spring boot application using eureka server? spring 启动微服务给:java.net.SocketException: Connection reset - spring boot micro-service giving: java.net.SocketException: Connection reset 保护微服务Spring Cloud安全性Oauth2 - Securing micro-service spring cloud security Oauth2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM