获取 Swagger 消息中的 HttpStatus ReasonPhrase 文本
Obtain HttpStatus ReasonPhrase text in Swagger message
将 org.apache.http
与
结合使用
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
在端点上描述 swagger 配置(虽然不仅仅是库的唯一目的),如下所示:
@ApiResponses(value = {
@ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Bad Request"),
@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "Server Error")
})
我想做的是用等效的枚举(据推测)替换 "Bad Request"
以避免每次都对消息进行硬编码。
Q1 - 我试图在 HTTP
包中寻找一个,但找不到。是否存在任何已知的 Enum/Class for reason phrase 来实现这一点?
Q2 - 另一种想法是,我可以从 HttpStatus.java
中获取变量名(因为变量名本身作为一个短语就足够了)。但再次怀疑的是 implementing reflection 就原因短语的业务逻辑而言可能成本更高。所以,简而言之,我主要是为了这个而主要关注第一季度。
注意 - 当然,由于事实,我不想为了获取原因短语。
我最后做的是添加一个 class 和等同于 org.apache.HttpStatus 整数代码的静态字符串包,比如 -
public class SwaggerMessage {
public static final String SC_OK = "OK";
public static final String SC_CREATED = "Created";
public static final String SC_ACCEPTED = "Accepted";
public static final String SC_NO_CONTENT = "No Content";
public static final String SC_MOVED_PERMANENTLY = "Moved Permanently";
public static final String BAD_REQUEST = "Bad Request";
public static final String SC_UNAUTHORIZED = "Unauthorized";
public static final String SC_FORBIDDEN = "Forbidden";
public static final String SC_NOT_FOUND = "Not Found";
public static final String SC_INTERNAL_SERVER_ERROR = "Server Error";
public static final String SC_NOT_IMPLEMENTED = "Not Implemented";
public static final String SC_SERVICE_UNAVAILABLE = "Service Unavailable";
}
注意 - 仍在寻求更清洁的解决方案,以上是我在实施服务中的需要。
将 org.apache.http
与
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
在端点上描述 swagger 配置(虽然不仅仅是库的唯一目的),如下所示:
@ApiResponses(value = {
@ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Bad Request"),
@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "Server Error")
})
我想做的是用等效的枚举(据推测)替换 "Bad Request"
以避免每次都对消息进行硬编码。
Q1 - 我试图在 HTTP
包中寻找一个,但找不到。是否存在任何已知的 Enum/Class for reason phrase 来实现这一点?
Q2 - 另一种想法是,我可以从 HttpStatus.java
中获取变量名(因为变量名本身作为一个短语就足够了)。但再次怀疑的是 implementing reflection 就原因短语的业务逻辑而言可能成本更高。所以,简而言之,我主要是为了这个而主要关注第一季度。
注意 - 当然,由于事实,我不想为了获取原因短语。
我最后做的是添加一个 class 和等同于 org.apache.HttpStatus 整数代码的静态字符串包,比如 -
public class SwaggerMessage {
public static final String SC_OK = "OK";
public static final String SC_CREATED = "Created";
public static final String SC_ACCEPTED = "Accepted";
public static final String SC_NO_CONTENT = "No Content";
public static final String SC_MOVED_PERMANENTLY = "Moved Permanently";
public static final String BAD_REQUEST = "Bad Request";
public static final String SC_UNAUTHORIZED = "Unauthorized";
public static final String SC_FORBIDDEN = "Forbidden";
public static final String SC_NOT_FOUND = "Not Found";
public static final String SC_INTERNAL_SERVER_ERROR = "Server Error";
public static final String SC_NOT_IMPLEMENTED = "Not Implemented";
public static final String SC_SERVICE_UNAVAILABLE = "Service Unavailable";
}
注意 - 仍在寻求更清洁的解决方案,以上是我在实施服务中的需要。