nullpointer nullpointer -4 years ago 51
Apache Configuration Question

Obtain HttpStatus ReasonPhrase text in Swagger message

Using

org.apache.http
with

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>


to depict the swagger configuration(not just the sole purpose of the library, though) on an endpoint as follows:

@ApiResponses(value = {
@ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Bad Request"),
@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "Server Error")
})


What I would want to do is replace the
"Bad Request"
with an equivalent Enumeration (supposedly) to avoid hardcoding the message every time.

Q1 - I tried looking out for one inside the
HTTP
package but couldn't find one. Is there any existing known Enum/Class for reason phrases to achieve this?

Q2 - The other way thought of is, I can get the variable name from the
HttpStatus.java
(since the variable name itself is good enough as a phrase). But the doubt again is implementing reflection could be costlier in terms of business logic for a reason phrase. So, in short, I am trying to focus on Q1 primarily for this.

Note - Of course, owing to the fact, that I would not want to explicitly define a dependency just for the sake of fetching the reason phrases.

Answer Source

What I ended up doing was adding a class with static string bundles equivalent to org.apache.HttpStatus integer codes, something like -

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";

}

Note - Seeking a cleaner solution still and the above is upto my need in the service implemented.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download