Are there any examples for integrating swagger 2.0 with Java EE (EJBs)

1,788 views
Skip to first unread message

Gary

unread,
Dec 23, 2015, 10:57:17 AM12/23/15
to Swagger
Hi,

I'm using RestEasy framework in JBOSS 7.1.1 for implementing REST API using EJBs. Is there any sample code for integrating swagger 2.0 with EJBs?

I've tried using Jax-rs documentation on swagger to integrate but I kept getting errors that /swagger.json couldn't be found. 


Thanks

Ron Ratovsky

unread,
Dec 23, 2015, 1:00:57 PM12/23/15
to swagger-sw...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary

unread,
Dec 23, 2015, 2:37:58 PM12/23/15
to Swagger
It is not working for me. Is there configuration to enable tracing in swagger to see if swagger is running into any issues at runtime?
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron Ratovsky

unread,
Dec 23, 2015, 2:55:22 PM12/23/15
to swagger-sw...@googlegroups.com
You can enable the debug logs, yes. However, what’s not working for you? What have you tried?

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Gary

unread,
Dec 23, 2015, 3:33:37 PM12/23/15
to Swagger


I’ve REST annotations on my interface,


@Path("/v1")

@Local

@Api(value = "/v1", description = "rest api")

public interface MyRestService

{


    @GET

    @Path("/token")

    @Produces("application/json")

    @ApiOperation(value = "Get token", response = String.class)

    public Response getToken();

}


this is implemented by following class,


@Stateless

public class MyServiceImpl implements MyRestService

{


    @EJB

    private TokenProviderLocal _tokenProvideLocal;


    @Override

    public Response getToken()

    {

        return _tokenProvideLocal.getToken();

    }

}


Swagger Bootstrap is defined as below,


public class Bootstrap extends HttpServlet {

    

    @Override

    public void init(ServletConfig config) throws ServletException {

        super.init(config);


        BeanConfig beanConfig = new BeanConfig();

        beanConfig.setVersion("1.0.0");

        beanConfig.setSchemes(new String[]{"http"});

        beanConfig.setHost("localhost:8180");

        beanConfig.setBasePath("/api");

        beanConfig.setResourcePackage("com.xxx.service");

        beanConfig.setScan(true);

    }

}


The web.xml is as follows,


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">


<context-param>

<param-name>resteasy.servlet.mapping.prefix</param-name>

<param-value>/</param-value>

</context-param>


<context-param>

<param-name>resteasy.jndi.resources</param-name>

<param-value>global/app/api/MyServiceImpl!com.xxx.service.MyRestService</param-value>

</context-param>


<context-param>

<param-name>resteasy.providers</param-name>

<param-value>

      io.swagger.jaxrs.listing.ApiListingResource,

      io.swagger.jaxrs.listing.SwaggerSerializers

  </param-value>

</context-param>


<listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> 

</listener> 


<servlet>

<servlet-name>resteasy-servlet</servlet-name>


<servlet-class>

org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>

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

</servlet>


<servlet-mapping>

<servlet-name>resteasy-servlet</servlet-name>

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

</servlet-mapping>


    <servlet>

        <servlet-name>Bootstrap</servlet-name>

        <servlet-class>com.xxx.service.Bootstrap</servlet-class>

        <load-on-startup>2</load-on-startup>

    </servlet>


<security-constraint>

<web-resource-collection>

<web-resource-name>my-rest</web-resource-name>

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

<http-method>GET</http-method>

<http-method>POST</http-method>

<http-method>DELETE</http-method>

</web-resource-collection>

<auth-constraint>

<role-name>user</role-name>

</auth-constraint>

</security-constraint>


<security-role>

<role-name>user</role-name>

</security-role>


<login-config>

<auth-method>BASIC</auth-method>

<realm-name>mySecure</realm-name>

</login-config>


</web-app>



When I load the war file in JBOSS, the web services are running fine but accessing /swagger.json given following 404 error,


Could not find resource for relative : /swagger.json of full path: http://myhost.com/api/swagger.json

Ron Ratovsky

unread,
Dec 23, 2015, 3:39:18 PM12/23/15
to swagger-sw...@googlegroups.com
Thanks for the details.

You mention that you try opening http://myhost.com/api/swagger.json however I don’t see you setting RESTEasy’s endpoint to be hosted under /api.
Is ‘api’ your application name and that’s why it’s part of the URL? Not sure how Jboss builds URLs (if it’s more like Tomcat or Jetty).

To be sure, if you assume that’s the URL because of the .setBasePath(…), then that’s not it. It has nothing to do with where your swagger.json is hosted.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Gary

unread,
Dec 23, 2015, 3:58:11 PM12/23/15
to Swagger
You are correct that /api is from the context-root of the war file.

The request http://localhost:8180/api/v1/token returns 200 OK with a token in the response body.

The request http://localhost:8180/api/swagger.json fails with 404,

Could not find resource for relative : /swagger.json of full path: http://myhost.com/api/swagger.json


Will switching on detailed logging in swagger provide any insight here?

Ron Ratovsky

unread,
Dec 29, 2015, 8:32:51 PM12/29/15
to swagger-sw...@googlegroups.com
It’ll definitely give us more insight, yes.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages