cancel
Showing results for 
Search instead for 
Did you mean: 

Swagger: How to stub REST service with Java code/annotations

SOLVED
New Contributor

Swagger: How to stub REST service with Java code/annotations

Greetings, All!

I'm using Swagger in my Java project. 

And I've got REST service, Java method with Swagger annotations:  (some data removed)

    @GET
    @Path("/data-by-district/{authtoken}/{country}/{districtid}")
    @ApiImplicitParams({
        @ApiImplicitParam(
            name = "authtoken",
            dataType = "string"
        ),
        @ApiImplicitParam(
            name = "country",
            dataType = "string"
        ),
        @ApiImplicitParam(
            name = "districtid",
            dataType = "long"
        )
    })
    @ApiOperation(value = "...", notes = "...")
    public MyObject getData(@PathParam("authtoken") String authtoken,
                                            @PathParam("country") String country,
                                            @PathParam("districtid") Long districtId) {
...

MyObject is actually the JPA Bean with nested objects/lists, etc. It works perfectly for receiving and sending back information. I've got Swagger annotations as well for my method and Swagger UI works as expected.

 

My question is: how can I easily stub this my method, so that UI developers could get a response like this:

 

{ //This is "MyObject
"id": "3567", //random number
"name": "random-string1",
"someNestedJpaObject": {
   "id": "123", //random number
   "position": "random-string2",
   "code":"random-string3",
   "myclassifier": {
   "key": "random-string4"
   }
}
}

Swagger - is a part of my application (Maven dependency).
So, I assume, it knows the structure of my (JPA) class and types of all fields.

Can I get something like mentioned above - a "quick" instance of MyObject, filled with random data?

 

I looked through all the "swagger code generators" around, but it looks like huge overkill in this case: I've already got swagger annotations (I can add an extra ones, if needed), data containers and types, but all the "stub-frameworks" wants me to create YAML files with a lot of information regarding the mock data...
I believe I don't need this extra work(yaml code), - I've got all the necessary info in Java code...no?!

 

 

6 REPLIES
Community Manager

Re: Swagger: How to stub REST service with Java code/annotations

Hi Community,

 

Any suggestions to dimedrol?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Highlighted
New Contributor

Re: Swagger: How to stub REST service with Java code/annotations

This pice of code might be helpful for your question. 

 

 @GET
 @Path("/findByStatus")
 @ApiOperation(value = "Finds Pets by status",
    notes = "Multiple status values can be provided with comma seperated strings",
    response = Pet.class,
    responseContainer = "List")
 public Response findPetsByStatus(...) { ... }

In the response parameter you can put a class, in your case: MyObject.class.
You can also add metadata to your model, with the @ApiModelProperty annotation, in class and parameters level. 

 

@ApiModel(value="DifferentModel", description="Sample model for the documentation")
class OriginalModel {...}
 @ApiModelProperty(value = "pet status in the store", allowableValues = "available,pending,sold")
  public String getStatus() {
    return status;
  }

 

You can see other useful annotations in this link: Wiki Annotations

 

I hope this info can help you to solve your doubts and don't hesitate in ask again if this doesn't work for you.

 

Have a nice day!

Community Manager

Re: Swagger: How to stub REST service with Java code/annotations

Hi @dimedrol,

Did @ralphdoe's reply help you? If it did, please click the Accept as Solution button next to the reply.

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Contributor

Re: Swagger: How to stub REST service with Java code/annotations

Thanks, ralphdoe!

Looks like what I wanted.

Community Hero

Re: Swagger: How to stub REST service with Java code/annotations

Understand that this thread is resolved.

Just wanted to add that if swagger code gen tool is used and build it, it automatically generates with skeleton classes and even you can run the app which could send some example values in response.


Regards,
Rao.
Community Manager

Re: Swagger: How to stub REST service with Java code/annotations

Thanks for adding this, Rao. Welcome back Smiley Happy

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
2018 NEWS
Top Kudoed Authors