Forum Discussion

dimedrol's avatar
dimedrol
New Contributor
6 years ago

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?!

 

 

  • 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!

  • ralphdoe's avatar
    ralphdoe
    New Contributor

    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!

      • nmrao's avatar
        nmrao
        Champion Level 3
        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.