簡體   English   中英

從Java應用程序訪問微服務

[英]Accessing a micro-service from Java application

我正在從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響應。

請幫忙。 可能是什么問題呢 ?

端點可能區分大小寫。 嘗試更改:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM