2

I am trying a small application to learn spring boot with swagger UI. When i run the program, i can open http://localhost:8080/api-docs which shows json output. But when i access http://localhost:8080/swagger/index.html, it gives Whitelabel Error Page.

What steps i followed:

1- Copied the "dist" folder from the swagger-ui project that is available in GitHub.

2- Renamed the "dist" to "swagger" and put it inside src/main/public

3- Changed the path in index.html to "/api-docs".

Below is the screenshot from EclipseLuna IDE and all the program codes,

enter image description here

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.rajkishan.learnSpring</groupId>
<artifactId>RestfulWithSpring</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>LearnRESTfulSpring</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.12.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <start-class>com.rajkishan.Application</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.mangofactory</groupId>
        <artifactId>swagger-springmvc</artifactId>
        <version>0.8.8</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Application.java

package com.rajkishan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * Spring Boot Starter Class
 *
 */

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Greeting.java is a Normal pojo as in spring-restful-demo provided in https://spring.io/guides/gs/rest-service/

GreetingController.java

package com.rajkishan;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(value = "/greeting", method = RequestMethod.GET, produces = "application/json")
    public Greeting greeting(@RequestParam (value = "name", defaultValue = "World") String name){
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

SwaggerConfig.java

package com.rajkishan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.wordnik.swagger.model.ApiInfo;

@Configuration
@EnableSwagger
public class SwaggerConfig {
private SpringSwaggerConfig swaggerConfig;

@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig){
    this.swaggerConfig = swaggerConfig;
}

@Bean
public SwaggerSpringMvcPlugin customSwaggerPlugin(){
    return new SwaggerSpringMvcPlugin(this.swaggerConfig).apiInfo(apiInfo())
            .includePatterns("/greeting/.*");
}

private ApiInfo apiInfo(){
    ApiInfo apiInfo = new ApiInfo("My Spring Application", 
            "Learning Spring Restful", "termsOfServiceUrl", "contact", "license", "licenseUrl");
    return apiInfo;
 }
}

Index.html

$(function() {
    var url = window.location.search.match(/url=([^&]+)/);
    if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
    } else {
        url = "/api-docs"
    }
/*Rest of the Code Follows...*/

Can you see where i am doing it wrong? Maybe I have Missed Something?

2
  • I guess you are using using spring boot app, did you add an interceptor of swagger Commented Jul 2, 2015 at 13:01
  • @Pulkit: No, i am not using any Interceptor. I tried to add swagger-ui to a spring RESTful tutorial in spring's website. Commented Jul 3, 2015 at 4:36

1 Answer 1

4

In your Application class remove all the annotations and replace it with @SpringBootApplication in spring-boot 1.2 and above and a combination of @EnableAutoConfiguration and @ComponentScan for boot versions below 1.2.x. In particular remove the @EnableWebMvc annotation as it is interfering with the spring boot auto configuration of the resources, i.e. the one that serves up swagger-ui.

Also, your swagger-springmvc dependency is very outdated you should use at least 1.0.2. On a related note have you considered moving using springfox? It's the next generation of swagger-springmvc and It also supports swagger 2.0.

Sign up to request clarification or add additional context in comments.

7 Comments

I am starting to learn these things. I do not know how to use spring fox. Can you please give me any leads where i can know how to use it? And how can i find out what is the latest version for "swagger-springmvc". Mainly what i do is follow tutorials i find from google search and try to learn from there.
This reference docs should get you started.
Also note that I've edited the answer for the version of springboot that you have in your Pom file. If removing the @EnableWebMvc annotation solves your problem, i would appreciate it if you mark this as your answer.
I did the changes. Now swaggerUI starts but i just see the message like "localhost:8080/api-docs" is loading but nothing happens. I think i made mistake somewhere. I will do a clean project again with spring boot 1.2.4 and spring 4.1.6 and let you know.
I tried with spring boot 1.2.4 and swagger-springmvc version 1.0.2. But i cannot access index.html file. If you want to see what i did: github.com/Raj-Kishan/RESTful-Swagger.git. I can open localhost:8080/api-docs but dont see any GET Method mapped. It just shows default urls.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.