Home > Back-end >  How to determine the URL to send requests to for a remote Springboot server?
How to determine the URL to send requests to for a remote Springboot server?

Time:11-05

I have successfully deployed a Springboot server to AWS Elastic Beanstalk, and I can access it via the browser's URL.

I would like to be able to send requests to it from Postman. I figured that I could use this code to print the URL that I will send requests to from Postman:

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            String s1 = InetAddress.getLoopbackAddress().getHostAddress();
            String hostedAt = s1   ":"   environment.getProperty("local.server.port");
            logger.info("**The server is hosted at: {}", hostedAt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

But, this prints **The server is hosted at 127.0.0.1:8080 even when deployed to AWS. So obviously, the localhost is not where I should be sending requests, so then where?

Using my browser, if I navigate to the URL of application, then I get the Whitelabel error page proving that it worked. This is the URL: http://spara.us-east-1.elasticbeanstalk.com/

I am new to this so its probably something dumb, but any guidance would help. Thanks!

CodePudding user response:

ServletUriComponentsBuilder. fromRequestUri(HttpServletRequest

should work if you talking about spring boot base URL

CodePudding user response:

What you have right now is the exposed URL of the application, which is:

http://spara.us-east-1.elasticbeanstalk.com/

It seems you have not set the base URL/Context path for your app. The default context path, if not set, is empty. It means, you should have been able to hit the URL above with Postman.

However, if you do need to set the base URL for your spring boot app in your application.properties, you would then add that to the URL above and hit via postman.

For Spring boot 2.x:

server.servlet.context-path=/your-base-url

Spring Boot 1.2 (<2.0):

spring.data.rest.basePath=/your-base-url
  • Related