Host your application on the Internet

Statement : The sole purpose of this post is to learn how to host your application to the Internet so that anyone can access it across the world.

Solution :

  • Sign up for the heroku account.
  • Download heroku cli to host you application from your local terminal.
  • Login to your account by using id and password through terminal by using below command –

heroku login

  • Create a new repo on your github account.
  • Now clone your repo on your local machine using the below command –

git clone https://github.com/guptakumartanuj/Cryptocurrency-Concierge.git

  • It’s time to develop your application. Once it is done, push your whole code to your github repo by using below commands –
  1. tangupta-mbp:Cryptocurrency-Concierge tangupta$ git add .
  2. tangupta-mbp:Cryptocurrency-Concierge tangupta$ git commit -m “First commit of cryptocurrency Concierge””
  3. tangupta-mbp:Cryptocurrency-Concierge tangupta$ git push
  • Now you are ready to crate a heroku app. Use the below command for the same –
cd ~/workingDir
$ heroku create
Creating app... done, ⬢ any-random-name
https://any-random-name.herokuapp.com/ | https://git.heroku.com/any-random-name.git
  • Now commit you application to heroku using the below command –

tangupta-mbp:Cryptocurrency-Concierge tangupta$ git push heroku master

  • It’s time to access your hosted application using the above highlighted url. But most probably you won’t be able to access the same. Make sure one instance of your hosted application is running. Use the below command to do the same –

heroku ps:scale web=1

  • In case, you are getting the below error while running the above command, then you need to make one file name Procfile with no extension and add the same to git repo. Then you need to push the repo to heroku again.

Scaling dynos… !

    Couldn’t find that process type.

  • In my case, to run my spring boot application, I have added the following command in the Procfile to run the application.

          web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/*.war

  • Finally your application should be up and running. In case, you are facing any issues while pushing or running your application, you can check the heroku logs which will help you to troubleshoot the issue by using below commands-

heroku logs –tail

Enjoy coding and Happy Learning 🙂 

 

Advertisements

Generate Swagger UI through Spring Boot Application.

Statement : Generate Swagger UI for the listing of all the REST APIs through Spring Boot Application.

Follow the below steps to generate the Swagger UI  through Spring Boot application –

  1. Add following dependency in pom.xml –

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.6.1</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.6.1</version>

</dependency>

  1.  Add the following piece of code in your main application class having the @EnableSwagger2 annotation.

@EnableSwagger2

@SpringBootApplication

public class MyApp {

public static void main(String[] args) {

SpringApplication.run(MyApp.class, args);

}

@Bean

  public Docket api() {

    return new Docket(DocumentationType.SWAGGER_2).select()

        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build()

        .pathMapping(“/”).apiInfo(apiInfo()).useDefaultResponseMessages(false);

  }

  @Bean

  public ApiInfo apiInfo() {

    final ApiInfoBuilder builder = new ApiInfoBuilder();

    builder.title(“My Application API through Swagger UI”).version(“1.0”).license(“(C) Copyright Test”)

        .description(“List of all the APIs of My Application App through Swagger UI”);

    return builder.build();

  }

}

  1. Add the below RootController class in your code to redirect to the Swagger UI page. In this way, you don’t need to put the dist folder of Swagger-UI in your  resources directory.

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

@Controller

@RequestMapping(“/”)

public class RootController {

@RequestMapping(method = RequestMethod.GET)

public String swaggerUi() {

return “redirect:/swagger-ui.html”;

}

}

  1. Being a final steps, add the @Api and @ApiOperation notation in all your RESTControllers like below –

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.http.HttpStatus;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseStatus;

import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

@RestController

@RequestMapping(“/hello”)

@Api(value = “hello”, description = “Sample hello world application”)

public class TestController {

@ApiOperation(value = “Just to test the sample test api of My App Service”)

@RequestMapping(method = RequestMethod.GET, value = “/test”)

// @Produces(MediaType.APPLICATION_JSON)

public String test() {

return “Hello to check Swagger UI”;

}

@ResponseStatus(HttpStatus.OK)

@RequestMapping(value = “/test1”, method = GET)

@ApiOperation(value = “My App Service get test1 API”, position = 1)

public String test1() {

System.out.println(“Testing”);

if (true) {

return “Tanuj”;

}

return “Gupta”;

}

}

Now your are done. Now to run your Spring Boot Application, go to browser and type localhost:8080. You will see Swagger UI having all the details of your REST APIs.

Happy Coding 🙂 Enjoy the source code of the above implementation in my git repo.

Continue reading “Generate Swagger UI through Spring Boot Application.”