简体   繁体   中英

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.
I am using appache HTTPClient.

I am having the microservice, a simple Spring controller that is to send files via ftp. Accepts a json object with fields for host, user password, etc... It works fine, when I test it with the swagger-ui. But, I need to be able to use it programmatically and from the java concole application. 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. But when I run a test java class (2nd code piece) no exceptions or errors, but I get a 404 response.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM