0

I have Spring Boot application in which I use "spring.mvc.servlet.path=/api/v1" to acces my endpoints. For example when I test my "users" endpoint in postman i Have: "http://localhost:8081/api/v1/users". And it works.

The problem is when I integrate Swagger for my documentation. And I try to acces my doc in swagger-u.html with "http://localhost:8081/api/v1/swagger-ui.html" it doesn`t work.

When I change my configuration from application.properties to "server.servlet.context-path=/api/v1" . The endpoint of Swagger works and my "http://localhost:8081/api/v1/users" doesn`t work.

Any sugestions of how may I resolve this problem ?

gizetoN
  • 15
  • 3
  • 10

2 Answers2

0

I was running different applications on the same server and I run into accessibility issues which forced me to add server context-path for these different applications.

Context path is a name with which a web application is accessed. By default, Spring Boot serves the content on the root context path (“/”).

However to avoid accessibility issues you can change this in the application.propeties file:

 server.servlet.context-path=/test

The servlet path represents the path of the main DispatcherServlet which is the front controller. The default value is similar to the context path (“/”). To change these value you can use:

  spring.mvc.servlet.path=/api/v1

With these changes you can access your api and swagger documentation endpoints as follows

http://localhost:8081/test/api/v1/users
http://localhost:8081/test/swagger-ui.html

The swagger documentation is exposed only through the context-path because it is not accessed through the front controller...

You can read more about this here https://www.baeldung.com/spring-boot-context-path https://www.baeldung.com/spring-context-vs-servlet-path

.

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
-1

context path vs servlet path?

http://localhost:8080/context/myServlet

If you look at the request URL for a servlet mapped at the path /myservlet, here http:// is the protocol, localhost is the server name, /context is the context path and /myServlet is the servlet path.

http://localhost:8080/context/myServlet/random

If you look at the request URL for a servlet mapped at the path /myservlet/*, here /myServlet is the servlet path and /random is extra path info...

ref : https://coderanch.com/t/474573/certification/ContextPath-ServletPath

spring boot's tomcat can only serve content below that context path

server.servlet.contextPath=/api/

Dispatcher servlet path allows you to register additional servlets on other paths

spring.mvc.servlet.path=/v1

spring.mvc.servlet.path=/v2

Two dispatch servlets active on: http://localhost:8081/api/v1 and http://localhost:8081/api/v2

try accessing - http://localhost:8081/api/swagger-ui.html"

For Open API

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/

for short.

Reference https://springfox.github.io/springfox/docs/snapshot/