从Java应用程序访问微服务

时间:2019-07-30 18:36:05

标签: java microservices

我正在从Java访问微服务,结果是404-找不到响应,而通过Swagger-ui同样可以正常工作。
我正在使用appache HTTPClient。

我有微服务,这是一个简单的Spring控制器,可以通过ftp发送文件。接受带有主机,用户密码等字段的json对象... 当我使用swagger-ui测试它时,它工作正常。 但是,我需要能够以编程方式从Java conconle应用程序中使用它。 以下是我拥有的代码

控制器代码

@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);
    }
}

这是测试的一部分

        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() );

运行此命令并使用swagger-ui时,我将文件传输到ftp。但是,当我运行测试Java类(第二个代码段)时,没有异常或错误,但是却收到404响应。

请帮忙。可能是什么问题?

1 个答案:

答案 0 :(得分:2)

端点可能区分大小写。尝试更改:

String postUrl = "http://localhost:8080/WinAPI/sendFiles" ;

String postUrl = "http://localhost:8080/WinAPI/SendFiles" ; // the endpoint described in the controller
相关问题