ApiResponse 在 Swagger 1 和Swagger 2中的不同

在1中随意写code ,

在 2中 会提示

com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: No matching constant for [1]
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2214)
    at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
    at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:57)
    at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:70)
    at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:89)
    at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:69)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:105)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:91)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:53)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:782)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:774)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:298)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.mortbay.jetty.plugin.JettyRunMojo.restartWebApp(JettyRunMojo.java:435)
    at org.mortbay.jetty.plugin.JettyRunMojo$1.filesChanged(JettyRunMojo.java:394)
    at org.eclipse.jetty.util.Scanner.reportBulkChanges(Scanner.java:691)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:551)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403)
    at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:353)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.IllegalArgumentException: No matching constant for [1]
    at org.springframework.http.HttpStatus$Series.valueOf(HttpStatus.java:524)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.isSuccessful(SwaggerResponseMessageReader.java:148)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.read(SwaggerResponseMessageReader.java:105)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.apply(SwaggerResponseMessageReader.java:74)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:113)
    at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:80)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:50)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:48)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3628)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2336)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2295)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
    ... 36 more
[WARNING] Failed startup of context o.m.j.p.JettyWebAppContext{/,[file:/E:/homeGit/zhangbaowei/home-news/autonews-webapi/src/main/webapp/, jar:file:/D:/JavaTools/maven/my_local_repository/io/springfox/springfox-swagger-ui/2.4.0/springfox-swagger-ui-2.4.0.jar!/META-INF/resources/]},file:/E:/homeGit/zhangbaowei/home-news/autonews-webapi/src/main/webapp/
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: No matching constant for [1]
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2214)
    at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
    at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:57)
    at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:70)
    at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:89)
    at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:69)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:105)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:91)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:53)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:782)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:774)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:298)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.mortbay.jetty.plugin.JettyRunMojo.restartWebApp(JettyRunMojo.java:435)
    at org.mortbay.jetty.plugin.JettyRunMojo$1.filesChanged(JettyRunMojo.java:394)
    at org.eclipse.jetty.util.Scanner.reportBulkChanges(Scanner.java:691)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:551)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403)
    at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:353)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.IllegalArgumentException: No matching constant for [1]
    at org.springframework.http.HttpStatus$Series.valueOf(HttpStatus.java:524)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.isSuccessful(SwaggerResponseMessageReader.java:148)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.read(SwaggerResponseMessageReader.java:105)
    at springfox.documentation.swagger.readers.operation.SwaggerResponseMessageReader.apply(SwaggerResponseMessageReader.java:74)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:113)
    at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:80)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:50)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:48)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3628)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2336)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2295)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
    ... 36 more

原来, 2中的 code 与 httpstatus 挂钩了,所以,只能写 httpstaus 相关的 code

  @ApiResponses(value = {
 
             @ApiResponse(code = 500, message = "成功"),  // 正确的            @ApiResponse(code = 302, message = "过一会儿再发"), // 正确的//            @ApiResponse(code = 2, message = "超过限定次数"), // 这个就是错误的了    })
时间: 2024-12-03 23:55:59

ApiResponse 在 Swagger 1 和Swagger 2中的不同的相关文章

swagger core 和 swagger ui 如何关联【窥探】

几个片段: package io.swagger.jaxrs.listing; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.Path;

swagger集成到springBoot 项目中

1 pom 文件加包依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version></dependency><!-- swagger-ui --><dependency> <groupId>io.spring

Swagger 在asp.net core中的应用2

Swagger是一个把api和注释生成一个可视(或可访问)的输出工具,关且还可以进行手工测试我们的api,解决了程序不想写文档的问题(哈哈). Swashbuckle.AspNetCore是用来解决asp.net core下的api文档,不但能称显UI,还可以在UI上进行测试. 如果在asp.net core中使用swagger,首先在nuget下安装Swashbuckle.AspNetCore,不过现在是预览版,一定要把"包括预发行版"打上勾. 同时还要添加三个引用: Swashbu

SpringMVC 中配置 Swagger 插件

一.简介 Swagger的目标是为REST API定义一个与语言无关的标准接口,允许用户发现和理解计算机服务的功能,而无需访问源代码.当通过Swagger正确定义时,用户可以用最少量的实现逻辑理解远程服务并与之交互.类似于低级编程所做的接口. 二.实现步骤 1.添加 Maven 依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifact

Web Api 2.0中使用Swagger生成Api文档的2个小Tips

当Web Api 2.0使用OAuth2授权时,如何在Swagger中添加Authorization请求头? Swagger说明文档支持手动调用Api, 但是当Api使用OAuth2授权时,由于没有地方可以输入授权Token, 导致响应结果一直是401没有授权. 解决方案: 在Swagger配置文件中,添加对请求头中Authorization的设置. 1. 在Api项目中添加一个新类AddAuthorizationHeader并实现IOperationFilter接口 public class

浅析如何在Nancy中使用Swagger生成API文档

原文:浅析如何在Nancy中使用Swagger生成API文档 前言 上一篇博客介绍了使用Nancy框架内部的方法来创建了一个简单到不能再简单的Document.但是还有许许多多的不足. 为了能稍微完善一下这个Document,这篇引用了当前流行的Swagger,以及另一个开源的Nancy.Swagger项目来完成今天的任务! 注:Swagger是已经相对成熟的了,但Nancy(2.0.0-clinteastwood)和Nancy.Swagger(2.2.6-alpha)是基于目前的最新版本,但目

Asp.Net Core中使用Swagger,你不得不踩的坑

很久不来写blog了,换了新工作后很累,很忙.每天常态化加班到21点,偶尔还会到凌晨,加班很累,但这段时间,也确实学到了不少知识,今天这篇文章和大家分享一下:Asp.Net Core中使用Swagger,你不得不踩的坑. 这篇文章着重讲几点: swagger 跨层注释问题 swagger Get请求传多个参数的问题 swagger Enum 注释问题 swagger api文档版本控制 第一步:搭建一个webapi项目或者mvc项目,引入swagger nuget 我创建项目,习惯性的先创建一个

ASP.NET Core Web API中使用Swagger

本节导航 Swagger介绍 在ASP.NET CORE 中的使用swagger ??在软件开发中,管理和测试API是一件重要而富有挑战性的工作.在我之前的文章<研发团队,请管好你的API文档>也专门阐述了通过文档管理工具,来保证API文档和代码的一致性,这样更加有助于团队的协作. ??以往我们总是通过第三方平台工具来管理我们的API文档,如eolinker.在测试方面,我们也会依赖fiddler,PostMan这样的工具. ??Swagger兼具了API文档管理和测试的功能,而且保证了代码和

ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介

参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio 与https://www.jianshu.com/p/349e130e40d5 当一个WebApi完成之后,书写API文档是一件非常头疼的事,因为不仅要写得清楚,能让调用接口的人看懂,又是非常耗时耗力的一件事.在之前的一篇随笔中(