Changing title and description of Swagger UI using Springfox
Asked Answered
J

2

12

I am building a Spring Boot application and documenting it using a Swagger UI using the Springfox Swagger UI. I've got everything documented, but want to customize the title and description but can't figure out how. For example, in this image:

enter image description here

the title is "Springfox petstore API" and the description is Lorem Ipsum. But in my Swagger UI, both the title and the description say "API Documentation". I have tried using the @SwaggerDefinition annotation, but it does not seem to do anything.

Jerrilyn answered 9/11, 2017 at 19:20 Comment(1)
For swagger 2.0 and later, see this answer: #54186336Garygarza
G
17

I recommend you to use swagger editor, then you can auto generate Spring Boot server from the API docs, using "Generate Server" option from Top Menu. It is really great for generating an API for the first time.

If you want to set from the YAML you must provide this fields at the info section:

info:
  version: "1.0.0"
  title: My API title
  description: "Awesome description"

From the code, check the generated classes from Swagger editor and compare it with your code. You could set description and title, setting the corresponding attributes on the ApiInfo Builder object, which is inside the class SwaggerDocumentationConfig.

Here you have the code:

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2017-10-05T14:03:51.916-03:00")

@EnableSwagger2
@Configuration
public class SwaggerDocumentationConfig {

    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("My API Title")
            .description("Awesome description")
            .license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .termsOfServiceUrl("")
            .version("1.0.0")
            .contact(new Contact("", "", "[email protected]"))
            .build();
    }

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mypackage.api"))
            .build()
            .apiInfo(apiInfo());
    }
}

If none of this makes sense to you, show me a little more of your code to know how you are using Springfox and I can help you better.

Bests regards!

Gabriellia answered 9/11, 2017 at 21:4 Comment(9)
I tried to generate a Spring server from the example given, but nothing happened. What is supposed to happen?Jerrilyn
Make sure your browser is not blocking pop-ups. By selecting the option "Generate-Server-> spring" from the main menu, you will download a zip with a SpringBoot project. Then you can undo it and compare it against your code. Keep in mind that much of the autogenerated code may not be useful for your particular case.Gabriellia
I've enabled pop-ups and still nothing happens.Jerrilyn
Also, I tried adding your code and it didn't do anything.Jerrilyn
Could you please give more details about how you are actually tagging your code, in order to enable Swagger? For example how your SpringBoot Main application looks?Gabriellia
Swagger editor online with the code generator option gives you a complete example about tagging your code but it is not the only code generator. Check this other code gen. If you still can not download the code you could generate the server using the Petstore JSON specification as an example.Gabriellia
Also this example about configure springfox in SpringBoot may be usefull for you.Gabriellia
I got it to work, I just needed to add a version. Thanks! Unfortunately the swagger editor still doesn't work.Jerrilyn
As I see it, the title gets set on the OpenAPI but the title of the SwaggerUI HTML page is still "Swagger UI"Padegs
C
0

This may be a cleaner and easy way and it's working

@OpenAPIDefinition(info = @Info(
        title = "My API definition",
        description = "All API definitions bla bla bla....",
        version = "1.0.0"))
public class MyController {}

enter image description here

Canterbury answered 13/11, 2023 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.