Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springfox @RestController naming

I'm getting some minor troubles using Springfox. I can't set a name to @RestController classes.

I'm using Spring boot and Swagger2.

The following code will produce a controller named "rest-status-controller" in springfox ui. I've expected a "Application Status" instead. Is there another config I'm not aware of ?

@Api("Application Status")
@RestController
@RequestMapping("/rest/status")
public class RestStatusController {

    @ApiOperation(value="Get components current status")
    @RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
    public String global() {
    //...
    }

    @ApiOperation(value="Get mysql current status")
    @RequestMapping(value="/mysql" method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON)
    public String mysql() {
    //...
    }
}
like image 423
Barium Scoorge Avatar asked Jun 22 '16 07:06

Barium Scoorge


People also ask

What is Springfox Swagger2?

Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser. To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.

How does Springfox work?

Springfox works by examining an application, once, at runtime to infer API semantics based on spring configurations, class structure and various compile time java Annotations.


2 Answers

Try to use the tags parameter of the @Api Annotation to change the name of the grouping of your RestController methods. Excerpt from the corresponding Java Doc (shortened):

/**
 * A list of tags for API documentation control.
 * Tags can be used for logical grouping of operations by resources or any other qualifier.
 */
String[] tags() default "";

In your case, just use:

@Api(tags = "Application Status")
@RestController
@RequestMapping("/rest/status")
public class RestStatusController {
    ...
}

This should group all documented operations from RestStatusController with the tag "Application Status".

like image 162
Fabian Nack Avatar answered Sep 29 '22 04:09

Fabian Nack


Use both tags and description to avoid controller name API grouping. For example:

@Api(
  tags="Application Status.",
  description = "Provides Application Status API's.
")
like image 36
Narendra Avatar answered Sep 29 '22 02:09

Narendra