wkennedy / swagger4spring-web

Swagger support for Spring MVC
89 stars 46 forks source link

Doesn't handle ResponseEntity return types well #5

Closed cbraun closed 11 years ago

cbraun commented 11 years ago

All of my controllers use the Spring 3.2 way of rest service production. That is they return objects that are wrapped in ResponseEntity. For example:

@RequestMapping(method = RequestMethod.GET) public ResponseEntity<List> getUsers() {

...

    return new ResponseEntity<List<UserResponseDTO>>(userList, HttpStatus.OK);

}

so the doc output looks like:

...... [{"httpMethod":"GET","responseClass":"ResponseEntity","nickname":"getUser","parameters":[{"name":"name","paramType":"path","required":true,"allowMultiple":false,"dataType":"string"}] ...

Ideally the parser would be able to detect the generic being used here as User.

wkennedy commented 11 years ago

This is a problem related to type erasure. Unfortunately, once a class is compiled, the generic type is removed. This leaves your ResponseEntity looking like ResponseEntity<List> at run time. One thing you might try is

     @ApiOperation(value = "Put your value here", notes = "Put your notes here", httpMethod = "PUT", responseClass = "UserResponseDTO")

However, this will leave off the JSON for ResponseEntity. Another alternative might be creating your own ResponseEntity class that all your DTO objects extend. This way those fields will always be included in your DTO objects and Swagger will be able to correctly generate the JSON you are looking for. We might be able to come up with some more alternatives if none of those work.