Swagger-codegen: Unable to run spring server generated from swagger editor

Created on 19 Aug 2016  路  10Comments  路  Source: swagger-api/swagger-codegen

Issue in generated spring server on swagger editor

I wrote a swagger in http://editor.swagger.io/#/ and generated spring server. When i try to run the spring server getting error. I am not able to run the spring server even for the examples provided in swagger editor.

Swagger declaration file content or url

just go Here
select File -> Open Examples -> select any example (I tried for twitter, default, echo, instagram
Go to Generate Server -> Spring - generates spring-server-generated zip file . Extract it and open in IntelliJ run the server ... Getting below error

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_73]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_73]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_73]
Unable to reproduce Spring help wanted

Most helpful comment

When running under IDEA using the tomcat server, it fails:
If you take out the dependency, the server will come up.

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

All 10 comments

I tried the echo spec but couldn't repeat the issue by running mvn test in the unzipped folder:

[INFO] ------------------------------------------------------------------------
[INFO] Building swagger-spring 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ swagger-spring ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ swagger-spring ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to /Users/williamcheng/Downloads/spring-server 2/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ swagger-spring ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/williamcheng/Downloads/spring-server 2/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ swagger-spring ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ swagger-spring ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.100 s
[INFO] Finished at: 2016-08-22T15:47:04+08:00
[INFO] Final Memory: 22M/310M
[INFO] ------------------------------------------------------------------------

I think the issue may have to do with running the code in IntelliJ.

When running under IDEA using the tomcat server, it fails:
If you take out the dependency, the server will come up.

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

The spring server is a spring-boot app. It embeds its own Tomcat and can be started as a simple jar. See the spring-boot doc for details. If you want a classic spring-mvc app, use the --library option (but if you don't know spring boot, you should give it a try as it's a great addition to spring)

Hi Christophe,
The default embedded app-server for Spring Boot is Tomcat. Spring Boot will pick tomcat even if you don't specify the tomcat dependency.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.5.RELEASE)

2016-09-16 08:47:16.265  INFO 66212 --- [           main] io.swagger.Swagger2SpringBoot            : Starting Swagger2SpringBoot on l0503978.nmdp.org with PID 66212 (/private/tmp/spring/target/classes started by pbashyal in /private/tmp/spring)
2016-09-16 08:47:16.268  INFO 66212 --- [           main] io.swagger.Swagger2SpringBoot            : No active profile set, falling back to default profiles: default
2016-09-16 08:47:16.327  INFO 66212 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@58a90037: startup date [Fri Sep 16 08:47:16 CDT 2016]; root of context hierarchy
2016-09-16 08:47:17.813  INFO 66212 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-09-16 08:47:17.825  INFO 66212 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-09-16 08:47:17.826  INFO 66212 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-09-16 08:47:17.921  INFO 66212 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/v2]     : Initializing Spring embedded WebApplicationContext
2016-09-16 08:47:17.921  INFO 66212 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1598 ms
2

The problem was Intellij IDEA was failing to start up the application when tomcat dependency was specified in the pom.xml file. If you removed it, you could start up the application from the IDE.

Oh, I see ! We are both packaging as jar and declaring Tomcat as provided (which actually removes the embedded tomcat). Normally you declare Tomcat as provided when you package as war to be deployed in an external container.
So we should remove this dependency as an executable jar is what most people will expect as default.

In my case, i've to remove <scope>provided</scope> from both spring-boot-starter-tomcat and javax.validation:validation-api to get IDEA to run correctly.

@D3Hunter thanks for sharing the fix/workaround.

@wing328 actually there's a fix to do here ! I totally forgot about this one... I can PR the fix

Thanks for the PR by @cbornet

@smile18 @pbashyal-nmdp @D3Hunter please pull the latest master to give it a try (or use the SNAPSHOT version mentioned in the project's README)

Just remove provider, everything is OK.

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

change to:

>

org.springframework.boot
spring-boot-starter-tomcat
`

Was this page helpful?
0 / 5 - 0 ratings