0

I am learning swagger using the example pet store in the swagger editor: https://editor.swagger.io/

The code for spring is generated and I does not change anything. However, everytime I run it on http://localhost:8080, it gives the error message

This localhost page can’t be found
No webpage was found for the web address: http://localhost:8080/
HTTP ERROR 404

But I believe I am supposed to see something like in the following website: https://petstore.swagger.io/ May I ask how to solve this issue? Many thanks.

I follow the URL in the controller class HomeController.

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}
Www
  • 53
  • 8

2 Answers2

2

You might be visiting the wrong URL. Try out http://localhost:8080/<base-url>/swagger-ui.html

Additionally, you could refer this link for a basic setup.

Vikas Adyar
  • 149
  • 1
  • 8
  • What is the base url – Www Jun 09 '21 at 14:25
  • But in readMe file: You can view the api documentation in swagger-ui by pointing to http://localhost:8080/ – Www Jun 09 '21 at 14:25
  • May I know In which framework are you trying to build your server? Base url is the piece of the URI which you set in your application which is a common string after your hostname, in your case `http://localhost:8080/` – Vikas Adyar Jun 09 '21 at 14:37
  • 1
    In your case the '/' url is directly redirected to 'swagger-ui.html'. Hence you can just use `http://localhost:8080/v2/` – Vikas Adyar Jun 09 '21 at 14:41
1

It appears the context path is v2.

You should be able to access the demo at: http://localhost:8080/v2/<endoint>

Try accessing: http://localhost:8080/v2/swagger-ui.html

The context path is defined in a configuration file titled application.properties located under src/main/resource. The file contains the following:

springfox.documentation.swagger.v2.path=/api-docs
server.contextPath=/v2
server.port=8080
spring.jackson.date-format=io.swagger.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

The context path is defined under server.contextPath.

Nicolasome
  • 341
  • 1
  • 3
  • 21
  • I can now open. But why? How do you know this? – Www Jun 09 '21 at 14:34
  • 1
    There's a spring configuration file called `application.properties` that defines the base URL (technically called the contextPath), under src/main/resource. I updated my answer with the content of the file. Let me know if you have issue locating the file in your own environment. – Nicolasome Jun 09 '21 at 14:39