nerdErg / swagger4jaxrs

This is a Grails plugin that adds Swagger support to document REST APIs of any Grails projects that use the Grails JAX-RS (JSR 311) plugin.
Apache License 2.0
10 stars 10 forks source link

Have to hard code url in basePath? #8

Closed pherris closed 10 years ago

pherris commented 10 years ago

Do I have to hard code this url in the applicationContext.properties? How do I change from localhost to www.whatever.com?

michaelrice commented 10 years ago

It uses the grails.serverURL so if you dont set it then it will try to figure it out. I normally only set that in development, but in most cases you shouldnt have to set it.

pherris commented 10 years ago

If I don't set it, when I attempt to hit the service from the swagger ui, the URL used is prefixed to /api-docs/api/... when the actual url is /api/... any thoughts on what I am doing wrong?

<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
    <property name="resourcePackage" value="swaggerDemo"/>
    <property name="version" value="0.01"/>
    <property name="basePath" value=""/>
    <property name="title" value="PFM REST API"/>
    <property name="description" value="a description here"/>
    <property name="contact" value="an@email.com"/>
    <property name="license" value="Apache 2.0 License"/>
    <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
    <property name="scan" value="true"/>
</bean>

Grails resource:

@Path('/api/authentication')
@Api(value="/api/authentication", description = "Facilitates REST login for the new PFM")
@Produces("application/json")
class AuthenticationResource extends BaseResource {

@GET
@Path("/mobileLogin")
@ApiOperation(value = "Get the encoded PFM2 session id to use to forward to the login page (Tomcat login, add pfm2 session).")
String getMobileLoginRepresentation(
    @ApiParam(value = "Company ID", required = true) @QueryParam("login") String cid, 
    @ApiParam(value = "Password", required = true) @QueryParam("password") String password) {
    def map = [cid:cid, password:password]
    return getJson(map)
}
}
michaelrice commented 10 years ago

Oh I see what you mean there. I miss understood. I have an idea of how to work around that. Since this is just making a bean you can do that yourself and define things like this in the bootstrap and then use the config to define values. I should be able to test that today, if not feel free to give that a try and report back.

Basically my idea is in boot strap use the applicationContext to define this bean we are making by hand, then use grails.serverURL as the baseUrl value.

michaelrice commented 10 years ago

Ok you will love this solution. Ill make a change to the docs and send the maintainer a pull request.

First ditch the applicationContext.xml stuff you added. It is not needed.

Second go to grails-app/conf/spring/resources.groovy and define the bean there like so:

import com.wordnik.swagger.jaxrs.config.BeanConfig
beans = {
    swaggerConfig(BeanConfig) {
        resourcePackage = "com.toastcoders.VmOpsTools"
        version = "1.0"
        basePath = grailsApplication.config.grails.serverURL
        title = "VmOpsTools"
        description = "Tools to make your life as a Vmware Admin easier"
        contact = "michael@michaelrice.org"
        license = "MIT"
        licenseUrl = "http://opensource.org/licenses/MIT"
        scan = true
    }
}

This will create the bean and allow you to use grails.serverURL

pherris commented 10 years ago

Michael, you are right - I do love this solution! It worked great once I updated my Config.groovy to include the grails.serverURL for my various environments. Thank you!!