首页 文章

Swagger(-ui)不显示操作

提问于
浏览
3

我有一个 Spring 天的网络应用程序,我已经加入了swagger和swagger-ui . 我添加了一个虚拟类来测试swagger:

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiError;
import com.wordnik.swagger.annotations.ApiErrors;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(value = "DummyController", description = "Dummy description for the controller")
@Controller
@RequestMapping(value = "/dummy")
public class DummyClassForSwagger {

    @ApiOperation(value = "First dummy output", httpMethod = "GET")
    @ApiErrors({@ApiError(code = 404, reason = "First dummy test") })
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public String dummyOutputOne() {
        return "Dummy output";
    }

    @ApiOperation(value = "Second dummy output", httpMethod = "GET")
    @ApiErrors({@ApiError(code = 404, reason = "Second dummy test") })
    @RequestMapping(value = "/second", method = RequestMethod.GET)
    @ResponseBody
    public String dummyOutputTwo() {
        return "Second dummy output";
    }
}

在构建/部署之后,我可以在swagger页面上看到虚拟类(参见附件1) . 问题是,"List operations"没有显示任何内容 . 原始输出如下:

<controllerDocumentation>
<apiVersion>1.0</apiVersion>
<apis>
<description>Dummy description for the controller</description>
<operations>
<deprecated>false</deprecated>
<errorResponses>
<code>404</code>
<reason>First dummy test</reason>
</errorResponses>
<httpMethod>GET</httpMethod>
<nickname>dummyOutputOne</nickname>
<notes/>
<responseClass>String</responseClass>
<summary>First dummy output</summary>
</operations>
<path>/dummy/first</path>
</apis>
<apis>
<description>Dummy description for the controller</description>
<operations>
<deprecated>false</deprecated>
<errorResponses>
<code>404</code>
<reason>Second dummy test</reason>
</errorResponses>
<httpMethod>GET</httpMethod>
<nickname>dummyOutputTwo</nickname>
<notes/>
<responseClass>String</responseClass>
<summary>Second dummy output</summary>
</operations>
<path>/dummy/second</path>
</apis>
<basePath>http://localhost:8080/mapserver/core</basePath>
<models/>
<resourcePath>/dummy</resourcePath>
<swaggerVersion>1.0</swaggerVersion>
</controllerDocumentation>

我认为,问题是缺少标签“操作”或类似的东西......但我不确定(我不知道,如何解决这个问题) . 有什么建议?

1 回答

  • 0

    我有一个类似的问题 . 我收到了错误 "SwaggerOperation handleIllegalArgumentsException is missing method." .

    调试后我发现 Controller 类中的 @ExceptionHandler 方法没有任何 swagger 注释,这导致了问题 .

    我评论了我的 ExceptionHandler 方法,并且swagger ui开始正常工作 .

    我现在想知道在spring mvc中使用哪些招摇注释用于 ExceptionHandler 方法 .

    我的控制器类中的 ExceptionHandler 方法是:

    @ExceptionHandler({IllegalArgumentException.class})
    public ResponseEntity<?> handleIllegalArgumentsException(IllegalArgumentException e)
    {
        LOGGER.error("IllegalArgumentException in user controller", e);
        return new ResponseEntity<>(new ErrorResponseDTO("BAD_REQUEST", e.getMessage()), HttpStatus.BAD_REQUEST);
    }
    

相关问题