swagger.json is not found

198 views
Skip to first unread message

Cheng Li

unread,
Sep 25, 2015, 11:24:27 AM9/25/15
to Swagger
I copy the web.xml and application.xml from jaxrs-sample.(this is for swagger 2.0)
The jar I am using is swagger-core 1.5.3, swagger-jaxrs 1.5.3 
 two problems generate
1. there is one error said as below, which is the scan property is wrong.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'swaggerConfig' defined in ServletContext resource [/META-INF/spring/rest.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'scan' threw exception; nested exception is java.lang.NoSuchMethodError: org.reflections.util.ClasspathHelper.forPackage(Ljava/lang/String;[Ljava/lang/ClassLoader;)Ljava/util/Collection;

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1493)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)

at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)

at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)

at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)

at java.util.concurrent.FutureTask.run(FutureTask.java:262)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'scan' threw exception; nested exception is java.lang.NoSuchMethodError: org.reflections.util.ClasspathHelper.forPackage(Ljava/lang/String;[Ljava/lang/ClassLoader;)Ljava/util/Collection;

at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:108)

at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)

... 22 more


2. when i remove the scan property , the server could up, however, when i search the /api/swagger.json. i got 404. could you help me here. thanks.

please see below configuration.


web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


  <display-name>Swagger-CXF-Sample</display-name>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/META-INF/spring/rest.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <listener>

    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

  </listener>

  <servlet>

    <display-name>CXF Service Servlet</display-name>

    <servlet-name>CXFServiceServlet</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

 <init-param>

      <param-name>swagger.api.basepath</param-name>

      <param-value>http://localhost:8080/api</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>


  <!-- mount the CXF servlet on /api -->

  <servlet-mapping>

    <servlet-name>CXFServiceServlet</servlet-name>

    <url-pattern>/api/*</url-pattern>

  </servlet-mapping>

</web-app>


rest.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"

  xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util"

  xmlns:context="http://www.springframework.org/schema/context"

  xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd

        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">


  <import resource="classpath:META-INF/cxf/cxf.xml" />


  <context:component-scan base-package="io.swagger.sample" />


  <!-- Swagger writers -->

  <bean id="swaggerWriter" class="io.swagger.jaxrs.listing.SwaggerSerializers" />

  <!-- <bean id="apiWriter" class="io.swagger.jaxrs.listing.ApiDeclarationProvider" /> -->


  <!-- JSON mapper -->

  <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />


  <!-- Exception mapper -->

  <bean id="exceptionMapper" class="io.swagger.sample.resource.SampleExceptionMapper" />


  <!-- Application resources -->

  <bean id="userResource" class="io.swagger.sample.resource.UserResource" />

  <bean id="petResource" class="io.swagger.sample.resource.PetResource" />

  <bean id="petStoreResource" class="io.swagger.sample.resource.PetStoreResource" />


  <!-- Swagger API listing resource -->

  <bean id="swaggerResource" class="io.swagger.jaxrs.listing.ApiListingResource" />


  <cxf:bus>

    <cxf:features>

      <cxf:logging />

    </cxf:features>

  </cxf:bus>


  <!-- this scans the classes for resources -->

  <bean id="swaggerConfig" class="io.swagger.jaxrs.config.BeanConfig">

    <property name="resourcePackage" value="io.swagger.sample.resource"/>

    <property name="version" value="1.0.0"/>

    <property name="host" value="localhost:8080"/>

    <property name="basePath" value="/api"/>

    <property name="title" value="Swagger Petstore"/>

    <property name="description" value="This is a app."/>

    <property name="contact" value="api...@swagger.io"/>

    <property name="license" value="Apache 2.0"/>

    <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>

    <!-- <property name="filterClass" value="ApiAuthorizationFilterImpl"/> -->

     <property name="scan" value ="true"/>

  </bean>


  <bean class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" init-method="create">

    <property name="address" value="/" />

    <property name="serviceBeans">

      <list>

        <ref bean="userResource" />

        <ref bean="petResource" />

        <ref bean="petStoreResource" />


        <ref bean="swaggerResource" />

      </list>

    </property>

    <property name="providers">

      <list>

        <ref bean="jsonProvider" />

        <ref bean="swaggerWriter" />

        <ref bean="exceptionMapper" />


        <!-- required for writing swagger classes -->

        <!-- <ref bean="resourceWriter" />

        <ref bean="apiWriter" /> -->

      </list>

    </property>

  </bean>

</beans>

Reply all
Reply to author
Forward
0 new messages