{new} @Response
The {@link oaj.http.annotation.Response @Response} annotation is used to identify schema information about an HTTP response.
It can be used in the following locations:
When the
When applied to an exception class, this annotation defines Swagger schema and information on non-200 return types.
The following example shows the
Custom exceptions can also extend from one of the predefined HTTP exceptions such as the {@link oajr.exception.Unauthorized} exception:
When applied type classes returned by a Java method, this annotation defines schema and Swagger information on the body of responses.
In the example above, we're using the Ok class which is defined like so:
Another example showing how a redirect can be defined:
The
The
When used in both locations, the annotation values are combined, but values defined on the
method return annotation take precedence over the values defined on the type.
The
In the above example, the
The {@link oaj.http.annotation.ResponseStatus @ResponseStatus} annotation can be used on
the method of a 200 (the default).
The {@link oaj.http.annotation.ResponseHeader @ResponseHeader} annotation can be used on
the method of a
The {@link oaj.http.annotation.ResponseBody @ResponseBody} annotation can be used on
the method of a
If a toString()).
By default, POJOs representing the body of the request are serialized using the Juneau serializer
matching the requesting Accept header.
The {@link oaj.oapi.OpenApiSerializer} class can be used to serialize response bodies using OpenAPI rules.
The following examples show part-schema-based serialization of response bodies:
The annotation can be used to define the format of the output using OpenAPI-based rules.
The attributes on this annotation are also used to populate the generated Swagger for the method.
For example, in the case of the InvalidLogin example above, the following Swagger is generated:
When the {@link oaj.http.annotation.Response#code() @Response(code)} value is specified, the HTTP status is automatically set to that value on the response regardless of how it's used.
The following two examples are equivalent: