Spring boot - springdoc custom url for swagger-ui - spring

I need to use a specific url for Swagger-ui. I have tried to use the property "springdoc.swagger-ui.path" but it only redirects.
application.propperties:
springdoc.swagger-ui.path=/helloWorld/swagger
Url in browser: http://localhost:8181/helloWorld/swagger
but the url changes to the following when hits enter button:
http://localhost:8181/manager/swagger-ui/index.html?configUrl=/manager/swagger/swagger.json/swagger-config
the question is, how can i make the path be only http://localhost:8181/helloWorld/swagger/index.html or http://localhost:8181/helloWorld/swagger once i've hit enter (I need the word "swagger-ui" and configUrl disappear)?
I`m uisng Springdoc and even tried with springfox
Pom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>
application.propperties:
springdoc.api-docs.path=/helloWorld/swagger/swagger.json
springdoc.swagger-ui.path=/helloWorld/swagger

I guess it's version dependent regarding the answer.
if you're using 2.8.0 and above, you could use following dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
And create a controller which will just redirect to the main swagger page, like this:
#Controller
public class SwaggerController {
#RequestMapping("/")
public String index() {
return "redirect:swagger-ui.html";
}
}
But if you use 2.6.1 or like that you can personalize your swagger configuration java class extending WebMvcConfigurerAdapter and overwriting the addViewController, like this:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController(MAIN_URL, SWAGGER_UI_URL).setContextRelative(true);
registry.addRedirectViewController(MAIN_URL + "/", SWAGGER_UI_URL).setContextRelative(true);
}
That's it. Both approaches used the same dependencies but with different versions.

Related

Missing Swagger documentation with Springdoc for #RepositoryRestResource

I've created annotated repository, but no Swagger definition is visible
#RepositoryRestResource(path = "InstanceUptime")
public interface InstanceUptimeRepository extends JpaRepository<InstanceUptime, Long> {
#Override
#RestResource(exported = false)
void delete(InstanceUptime entity);
}
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.6.14</version>
</dependency>
Do I need to do something more to make it working?
According to the Spring docs:
The projects that use spring-data-rest can add the following
dependency (springdoc-openapi-data-rest) in combination with the springdoc-openapi-ui
dependency.
So both dependencies should be added to the pom.xml:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.14</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.6.14</version>
</dependency>
Yes it was missing dependency. I was missing spring-data-rest.

apigateway couldn't display swagger-ui documentations with spring boot 3

I'm building microservices project using spring boot 3 and I have found some problems from api gateway.
when I type each service port I get swagger ui documentation but when I try access it's documentation from api the gateway I get this error "Failed to load remote configuration."
here is my springdoc config for
api-gatway :
<!-- Swagger documentation Dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-common</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
gateway config:
#Configuration
#Primary
public class SwaggerConfig {
public static final String API_URI = "/v3/api-docs";
private final RouteDefinitionLocator routeLocator;
public SwaggerConfig(RouteDefinitionLocator routeLocator) {
this.routeLocator = routeLocator;
}
private SwaggerResource swaggerResource(String name, String location) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("1.0");
return swaggerResource;
}
}
gateway routing :
#Bean
DiscoveryClientRouteDefinitionLocator dynamicRoutes(ReactiveDiscoveryClient rdc, DiscoveryLocatorProperties dlp){
return new DiscoveryClientRouteDefinitionLocator(rdc,dlp);
}
backend services :
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
expecting : enter image description here
but what i get
enter image description here
enter image description here

Why Spring Boot 4 "custom error page" fails 404?

I tried to implement an own error page handling.But my page doesnt show up.
Controller:
#Controller
public class MyCustomErrorController implements ErrorController {
#RequestMapping(value = "/error", method = RequestMethod.GET)
public String handleError() {
return "error";
}
#Override
public String getErrorPath() {
return "/error";
}}
I did my own error.html file in src/main/resources/static/html.
The html folder is created by myself. Where is the problem?
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
</dependencies>
You can open your html file from static content like
localhost:8080/yourpagename
By default, Spring boot serves index.html as the root resource when accessing the root URL of a web application.
(yourhtml).html should exist under any of these paths:
src/main/resources/META-INF/resources/home.html
src/main/resources/resources/home.html
src/main/resources/static/home.html
src/main/resources/public/home.html
In order to view your error.html static page you need to return “error.html” in controller
In order to define your own static resource locations
you could use this property in application.properties or application.yml file :
spring.resources.static-locations=your own locations

Swagger Apache CXF JAX-RS Maven

I am having trouble using swagger with Apache CXF, JAX-RS services.
beans.xml:
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
<bean id="apiListingResourceJSON" class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON"/>
<bean id="apiDeclarationProvider" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider"/>
Example use:
#Path("/")
#Api(value="/", description="VenturoSoft eMustering Services")
public class Service {
final static Logger logger = Logger.getLogger(Service.class);
#GET
#Path("/echo/{input}")
#Produces(MediaType.TEXT_PLAIN_VALUE)
#Consumes(MediaType.TEXT_PLAIN_VALUE)
#ApiOperation(value = "Get Ping", response = String.class)
public String ping(#PathParam("input") String input) {
return PingImpl.ping(input);
}
Pom.xml:
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.10</artifactId>
<version>1.3.12</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
Run:
mvn tomcat7:run-war
But when I load:
http://localhost:13000/jaxrs-service/api
I get no response.
http://localhost:13000/jaxrs-service/echo/echoSomething
Works as desired.
You're looking at some very old dependencies. From your code, it looks like you're using JAXRS. If that's the case, the latest dependencies should be:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.4</version>
</dependency>
Please follow the JAXRS sample here:
https://github.com/swagger-api/swagger-samples/tree/master/java/java-jaxrs-cxf
Which should show you how to correctly configure the server.

How to change HAL links format using Spring HATEOAS

I'm building a Spring REST application using Spring HATEOAS (0.16.0.RELEASE) and I'd like the JSON links output to look like:
_links: {
self: {
href: "https://<ip>/api/policies/321"
}
}
while it renders like:
"links":
[{
"rel":"self",
"href":"http://<ip>/api/policies/321"
}]
I'm using HATEOAS Resource and ResourceAssembler.
Why do I get this format instead of the other? How can I change it?
In order to use HAL as the message format language for our RESTful
API, and enable automatic pagination, we need some configuration
changes in our applicaiton. Since Spring Data and Spring HATEOAS
already provides annotations for configuration, all we need is to add
those annotations:
#Configuration
#EnableWebMvc
#EnableSpringDataWebSupport
#EnableHypermediaSupport(type = { HypermediaType.HAL })
#ComponentScan(basePackages = {
"com.jiwhiz.rest"
})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer c) {
c.defaultContentType(MediaTypes.HAL_JSON);
}
}
#EnableSpringDataWebSupport will add support for pagination and
#EnableHypermediaSupport(type = { HypermediaType.HAL }) will add
hypermedia support. Then we set default content type to
application/hal+json.
cite: Design and Build RESTful API with Spring HATEOAS by Yuan Ji
Make sure that your using com.fasterxml.jackson dependency instead of others like org.codehaus.jackson. For example, in a Maven pom.xml :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.3</version>
</dependency>

Resources