Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc springfox在所有HTTP方法上生成REST操作_Spring Mvc_Swagger_Springfox - Fatal编程技术网

Spring mvc springfox在所有HTTP方法上生成REST操作

Spring mvc springfox在所有HTTP方法上生成REST操作,spring-mvc,swagger,springfox,Spring Mvc,Swagger,Springfox,我正试图将springfox 2.6.1(springfox-swagger 2和springfox swagger ui)与Spring MVC应用程序一起使用,以生成swagger文档。我的应用程序有很多现有的RESTAPI,但是我现在只想炫耀其中的一个。它类似于以下内容: @RestController public class MyController { @RequestMapping(value = "/getapi/{key}", produces = MediaType

我正试图将springfox 2.6.1springfox-swagger 2springfox swagger ui)与Spring MVC应用程序一起使用,以生成swagger文档。我的应用程序有很多现有的RESTAPI,但是我现在只想炫耀其中的一个。它类似于以下内容:

@RestController
public class MyController {

    @RequestMapping(value = "/getapi/{key}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "get something", response = MyEntity.class)
    @ApiResponse(code = 404, message = "Not found")
    public ResponseEntity<String> find(@ApiParam(value = "", required = true) @PathVariable String key) {
        ...
    }

使用您不想公开的@apignore注释您的方法,然后再试一次。

我找不到发生这种情况的确切原因,但是我可以通过向rest控制器添加注释
io.swagger.annotations.Api
并将摘要另外修改为:

    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .build()
            .apiInfo(apiInfo());

如果在请求映射中没有指定HTTP谓词,SpringFox默认为所有HTTP谓词生成swagger。在控制器的'find'方法中,指定请求方法。这会解决你的问题

例如:

@RequestMapping(value = "/getapi/{key}", 
method=RequestMethod.GET, 
produces = MediaType.APPLICATION_JSON_VALUE)

控制器只有一种方法。为什么我要将@ApiIgnore添加到它?生成所有http方法的原因是springfox无法从RequestMapping注释中检测到该方法。添加一个显式方法,在本例中为注释添加GET,例如:@RequestMapping(value=“/getapi/{key}”,products=MediaType.APPLICATION\u JSON\u value,method=RequestMethod.GET)是的,这对我有效。如果我们没有为RequestMapping指定method属性,那么SpringFox会为所有http谓词生成swagger。
@RequestMapping(value = "/getapi/{key}", 
method=RequestMethod.GET, 
produces = MediaType.APPLICATION_JSON_VALUE)