swagger-springmvc-demo icon indicating copy to clipboard operation
swagger-springmvc-demo copied to clipboard

Not getting things working with Spring Boot

Open wimdeblauwe opened this issue 11 years ago • 13 comments

Hi,

I am trying to integrate Swagger and Swagger UI in my spring boot app. I am using Spring Boot 1.2.1 with Java 8. I have Swagger working, because I get proper JSON back when going to http://localhost:8080/api-docs

However, I don't get nice HTML as I was hoping I would get. Please take a look at my test project here: https://github.com/wimdeblauwe/springboot-swagger-test

I would be most grateful if you could tell me what is wrong.

I also posted this question on SO: http://stackoverflow.com/questions/27861872/unable-to-get-swagger-ui-working-with-spring-boot

wimdeblauwe avatar Jan 10 '15 15:01 wimdeblauwe

I got similar issues with Swagger UI. This issue is related to Spring Boot JSP limitations. I opened a PR to use HTML file instead of JSP file, which should fix this issue.

alexcheng1982 avatar Jan 13 '15 00:01 alexcheng1982

Thanks for the PR, hopefully it will be fixed soon.

wimdeblauwe avatar Jan 20 '15 19:01 wimdeblauwe

@alexcheng1982 I am also having this same issue, until #16 is fixed, is there some workaround (even manual) to make this work? I am not sure how to manually do what you changes do. thanks!

jmlucjav avatar Jan 20 '15 19:01 jmlucjav

Hi there, we are looking for a workaround too, has anyone found anything?

fpeloi-bmj avatar Mar 03 '15 14:03 fpeloi-bmj

I faced the similar issue. After going through discussion over stackflow, I created a sample project at github. The Swagger-UI works, but I have trouble to get API documents. It also works for Tomcat only.

WeipingGuo avatar Mar 07 '15 21:03 WeipingGuo

I finally made it work with Spring Boot for Tomcat. Please see github, unfortunately it still does not work for Jetty. I got error "org.apache.jasper.servlet.JspServlet : PWC6117: File "null" not found" for http://localhost:8080/sdoc.jsp

WeipingGuo avatar Mar 20 '15 18:03 WeipingGuo

I'm using the following workaround. It hides the issue by forwarding / to /sdoc.jsp. Not sure if that's how it's supposed to be, but it seems to be working for me:

@Configuration
public class DefaultView  extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/sdoc.jsp");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

You do end up getting something ugly like "http://localhost:8080${pageContext.request.contextPath}/api-docs" in the text box. To fix that, I added the following to my build.grade:

runtime 'org.apache.tomcat.embed:tomcat-embed-jasper'

If you are using maven:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>    
    <scope>provided</scope>
</dependency>

~~UPDATE: I guess I jumped the gun. It loads up the API fine, but when you attempt to test the endpoints, you get a 404 because it builds the path off http://localhost:8080/api-docs. I assume it's probably due to mucking around with the path like I did above. I'll have to dig into this some more.~~

The errors I encountered were related to the version of Swagger-UI that comes with org.ajar:swagger-spring-mvc-ui:0.4 and not related to the changes I made. Hence the workaround I described should still work.

vivin avatar Mar 24 '15 22:03 vivin

I have made it work for Tomcat. For Jetty, I have to copy dist to a resource folder and access via index.html. Please see https://github.com/WeipingGuo/swagger-spring-boot-example

WeipingGuo avatar Mar 25 '15 00:03 WeipingGuo

@WeipingGuo Can you tell me what changes you made to get it to work? I don't see anything very different inside Application.java from how I have it set up. I added the @EnableAutoConfiguration annotation like you have, but it didn't make a difference for me.

vivin avatar Mar 25 '15 15:03 vivin

@vivin I just ran it and it works for me. Nothing special from Application.java, but I do have the following in the pom.xml

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
</dependency>

WeipingGuo avatar Mar 25 '15 17:03 WeipingGuo

@WeipingGuo Interesting. Yes, the tomcat-embed-jasper stuff is to get rid of the ugly EL stuff that shows up in the text box for the API url. For whatever reason, the way you have it set up doesn't work foe me; I still get the Whitelabel Error page.

Using the method I have described above redirects to sdoc.jsp, but errors when I attempt to test the API endpoints. Another thing I noticed is that the textbox where you can enter the request-body schema is just a text-field now, and clicking on the schema doesn't populate that field. I'll try experimenting some more.

vivin avatar Mar 25 '15 17:03 vivin

@vivin that is weird. can you clone my project and run mvn spring-boot:run? something is missing :-)

WeipingGuo avatar Mar 25 '15 18:03 WeipingGuo

@WeipingGuo It appears that the issue I'm facing (with wrong request URLs) is related to this. Now I'm using org.ajar:swagger-spring-mvc-ui:0.4 and the version of Swagger-UI in that is 2.0.17. This particular bug has been fixed in Swagger-UI 2.0.19 and hence I believe that might be the cause.

vivin avatar Mar 25 '15 18:03 vivin