Skip to content

BeanConfig needs some examples without Spring #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rage-shadowman opened this issue Jan 7, 2014 · 11 comments
Closed

BeanConfig needs some examples without Spring #412

rage-shadowman opened this issue Jan 7, 2014 · 11 comments

Comments

@rage-shadowman
Copy link

BeanConfig seems to be touted as the answer to our configuration problems, allowing scanning of packages for annotated classes (see issues #173, #174 and #175).

It would be very helpful if we had some examples of how to use it. But the only examples I could find were using Spring. Please create some generic examples.

I've tried just creating a new one in the PostConstruct of a Singleton/Startup bean, but none of my resources are found, so I must be doing something wrong.

I initialize my BeanConfig like this:

      BeanConfig beanConfig = new BeanConfig();
      beanConfig.setVersion( "1.0" );
      beanConfig.setScan( true );
      beanConfig.setResourcePackage( MyResource.class.getPackage().getName() );
      beanConfig.setBasePath( "http://localhost:9080/v1/api" );
      beanConfig.setDescription( "My RESTful resources" );
      beanConfig.setTitle( "My RESTful API" );

All I get on localhost:9080/v1/api/api-docs is this:

{"apiVersion":"1.0","swaggerVersion":"1.2"}

I don't get anything from MyResource (which has Api annotation on the class and ApiOperation annotation on the GET resource).

@rage-shadowman
Copy link
Author

PS - I also tried initializing a BeanConfig instance in the Application constructor (the constructor of the class which extends javax.ws.rs.core.Application and has the javax.ws.rs.Application annotation on it), but that doesn't seem to work any better.

@fehguy
Copy link
Contributor

fehguy commented Jan 7, 2014

please try beanConfig.setScan(true) as the last method

@rage-shadowman
Copy link
Author

That gets me more information in api-docs, but still does not include any resources.

{
   "apiVersion": "1.0",
   "swaggerVersion": "1.2",
   "info": {
      "title": "My RESTful API",
      "description": "My RESTful resources"
   }
}

I added whitespace to the results.

@rage-shadowman
Copy link
Author

Our web.xml is empty. All RESTful resources are determined by RESTEasy scanning packages for @Api annotation and the Application is also determined by scanning (not sure if it scans for implentations of Application or for the @ApplicationPath annotation, but our Application is an extremely simple class).

package my.restful.service;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath( "/api" )
public class MyRestApplication
   extends Application
{
}

We can add a constructor to setup the BeanConfig if that's where it should happen.

import my.restful.service.resources.MyResource;

...

   public MyRestApplication ()
   {
      System.out.println( "Initializing Swagger BeanConfig" );
      BeanConfig beanConfig = new BeanConfig();
      beanConfig.setVersion( "1.0" );
      beanConfig.setResourcePackage( MyResource.class.getPackage().getName() );
      beanConfig.setBasePath( "http://localhost:9080/api" );
      beanConfig.setDescription( "My RESTful resources" );
      beanConfig.setTitle( "My RESTful API" );
      beanConfig.setScan( true );
   }

@rage-shadowman
Copy link
Author

Also, I am using the following dependencies in my pom (in case I'm using the wrong versions):

      <dependency>
         <groupId>com.wordnik</groupId>
         <artifactId>swagger-annotations</artifactId>
         <version>1.3.1</version>
      </dependency>
      <dependency>
         <groupId>com.wordnik</groupId>
         <artifactId>swagger-core_2.10</artifactId>
         <version>1.3.1</version>
      </dependency>
      <dependency>
         <groupId>com.wordnik</groupId>
         <artifactId>swagger-jaxrs_2.10</artifactId>
         <version>1.3.1</version>
      </dependency>

@fehguy
Copy link
Contributor

fehguy commented Jan 7, 2014

if you can send a pull-request with a sample with no web.xml, i'll get it to work, document it, and put it in the samples repo. There are a bunch of different ways to do so without web.xml

@rage-shadowman
Copy link
Author

Is that what you were asking for?

I am able to use this to debug it in JBoss7 (technically EAP 6.1) but the api-docs is missing any references to the resources. By default JBoss7 uses port 8080, but I set mine up to use 9080 to make it play well with Tomcat so the base path might need to change once it is actually being used.

@fehguy
Copy link
Contributor

fehguy commented Jan 7, 2014

I'm going to check it out in a bit and will let you know if I have questions--thx for pushing that

@rage-shadowman
Copy link
Author

awesome, thanks for looking into it

@rage-shadowman
Copy link
Author

I finally tracked this down to an issue in the org.reflections code.

BeanConfig uses the result of ClasspathHelper.forPackage(resourcePackage) which in JBoss7 returns a URL in the form of "vfs:/path/to/directory/WEB-INF/classes". However, Reflections uses Vfs.fromUrl which (in version 0.9.8 -- the version swagger depends on) cannot handle "vfs:" URLs.

I forced my application to depend on v0.9.9-RC1 (which recognizes the "jboss_vfs" URL type) and all is happy now.

         <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.9-RC1</version>
         </dependency>

@danielflower
Copy link

I spent ages trying to bypass BeanConfig, classpath scanning, reflection, Spring, XML and anything that wasn't plain java specifying what to configure. The result is here in case anyone else is interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants