public class RestHandlerBuilder extends Object implements MuHandlerBuilder<RestHandler>
RestHandler
for handling JAX-RS REST resources.restHandler(Object...)
Constructor and Description |
---|
RestHandlerBuilder(Object... resources) |
Modifier and Type | Method and Description |
---|---|
<P> RestHandlerBuilder |
addCustomParamConverter(Class<P> paramClass,
javax.ws.rs.ext.ParamConverter<P> converter)
Registers a parameter converter class that convert strings to and from a custom class.
|
RestHandlerBuilder |
addCustomParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider paramConverterProvider)
Registers an object that can convert rest method parameters (e.g.
|
<T> RestHandlerBuilder |
addCustomReader(javax.ws.rs.ext.MessageBodyReader<T> reader)
Registers an object that can deserialise request bodies into custom classes.
|
<T> RestHandlerBuilder |
addCustomWriter(javax.ws.rs.ext.MessageBodyWriter<T> writer)
Registers an object that can write custom classes to responses.
|
<T extends Throwable> |
addExceptionMapper(Class<T> exceptionClass,
javax.ws.rs.ext.ExceptionMapper<T> exceptionMapper)
Adds a mapper that converts an exception to a response.
|
RestHandlerBuilder |
addRequestFilter(javax.ws.rs.container.ContainerRequestFilter filter)
Registers a request filter, which is run before a rest method is executed.
|
RestHandlerBuilder |
addResource(Object... resources)
Adds one or more rest resources to this handler
|
RestHandlerBuilder |
addResponseFilter(javax.ws.rs.container.ContainerResponseFilter filter)
Registers a response filter, which is called after execution of a method takes place.
|
RestHandler |
build() |
static RestHandler |
create(Object... resources)
Deprecated.
Use restHandler(resources).build() instead.
|
static RestHandlerBuilder |
restHandler(Object... resources)
Creates a handler builder for JAX-RS REST services.
|
RestHandlerBuilder |
withCORS(CORSConfig corsConfig)
Specifies the CORS config for the REST services.
|
RestHandlerBuilder |
withCORS(CORSConfigBuilder corsConfig)
Specifies the CORS config for the REST services.
|
RestHandlerBuilder |
withDocumentation()
Deprecated.
This does nothing. To expose API endpoints, use
withOpenApiJsonUrl(String) and/or withOpenApiHtmlUrl(String) |
RestHandlerBuilder |
withOpenApiDocument(OpenAPIObjectBuilder openAPIObject)
Use this value to create JSON and HTML documentation for your rest service.
|
RestHandlerBuilder |
withOpenApiHtmlCss(String css)
When using the HTML endpoint made available by calling
withOpenApiDocument(OpenAPIObjectBuilder)
this allows you to override the default CSS that is used. |
RestHandlerBuilder |
withOpenApiHtmlUrl(String url)
Enables a simple HTML endpoint that documents the API exposed by the rest resources declared by this builder.
|
RestHandlerBuilder |
withOpenApiJsonUrl(String url)
Enables an Open API JSON URL at the specified endpoint.
|
public RestHandlerBuilder(Object... resources)
public RestHandlerBuilder addResource(Object... resources)
resources
- One or more instances of classes that are decorated with Path
annotations.public <T> RestHandlerBuilder addCustomWriter(javax.ws.rs.ext.MessageBodyWriter<T> writer)
Registers an object that can write custom classes to responses.
For example, if you return an instance of MyClass
from a REST method, you need to specify how
that gets serialised with a MessageBodyWriter<MyClass>
writer.
T
- The type of object that the writer can serialisewriter
- A response body writerpublic <T> RestHandlerBuilder addCustomReader(javax.ws.rs.ext.MessageBodyReader<T> reader)
Registers an object that can deserialise request bodies into custom classes.
For example, if you specify that the request body is a MyClass
, you need to specify how
that gets deserialised with a MessageBodyReader<MyClass>
reader.
T
- The type of object that the reader can deserialisereader
- A request body readerpublic RestHandlerBuilder addCustomParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider paramConverterProvider)
Registers an object that can convert rest method parameters (e.g. querystring, header, form or path params) into custom classes.
In most cases, it is easier to instead use addCustomParamConverter(Class, ParamConverter)
paramConverterProvider
- A provider of parameter converterspublic <P> RestHandlerBuilder addCustomParamConverter(Class<P> paramClass, javax.ws.rs.ext.ParamConverter<P> converter)
Registers a parameter converter class that convert strings to and from a custom class.
This allows you to specify query string parameters, form values, header params and path params as custom classes.
For more functionality, addCustomParamConverterProvider(ParamConverterProvider)
is also available.
P
- The type of the parameterparamClass
- The class that this converter is meant for.converter
- The converterpublic RestHandlerBuilder withOpenApiJsonUrl(String url)
url
- The URL to serve from, for example /openapi.json
or null
to disable the JSON endpoint. Disabled by default.withOpenApiDocument(OpenAPIObjectBuilder)
,
withOpenApiHtmlUrl(String)
public RestHandlerBuilder withOpenApiHtmlUrl(String url)
url
- The URL to serve from, for example /api.html
or null
to disable the HTML endpoint. Disabled by default.withOpenApiDocument(OpenAPIObjectBuilder)
,
withOpenApiJsonUrl(String)
,
withOpenApiHtmlCss(String)
public RestHandlerBuilder withOpenApiHtmlCss(String css)
withOpenApiDocument(OpenAPIObjectBuilder)
this allows you to override the default CSS that is used.css
- A string containing a style sheet definition.public RestHandlerBuilder withOpenApiDocument(OpenAPIObjectBuilder openAPIObject)
Use this value to create JSON and HTML documentation for your rest service.
Minimal example:
OpenAPIObjectBuilder.openAPIObject()
.withInfo(InfoObjectBuilder.infoObject()
.withTitle("Mu Server Sample API")
.withVersion("1.0")
.build())
Extended example:
OpenAPIObjectBuilder.openAPIObject()
.withInfo(InfoObjectBuilder.infoObject()
.withTitle("Mu Server Sample API")
.withVersion("1.0")
.withLicense(LicenseObjectBuilder.Apache2_0())
.withDescription("This is the **description**\n\nWhich is markdown")
.withTermsOfService(URI.create("http://example.org/terms/"))
.build())
.withExternalDocs(externalDocumentationObject()
.withDescription("Full documentation")
.withUrl(URI.create("http://example.org/docs"))
.build())
The path information and operation information will be automatically generated. By default, you can access
the Open API specification of your rest service at /openapi.json
or view the HTML at
/api.html
openAPIObject
- An API Object builder with the OpenAPIObjectBuilder.withInfo(InfoObject)
set.OpenAPIObjectBuilder.openAPIObject()
,
withOpenApiJsonUrl(String)
,
withOpenApiHtmlUrl(String)
public <T extends Throwable> RestHandlerBuilder addExceptionMapper(Class<T> exceptionClass, javax.ws.rs.ext.ExceptionMapper<T> exceptionMapper)
Adds a mapper that converts an exception to a response.
For example, you may create a custom exception such as a ValidationException that you throw from your jax-rs methods. A mapper for this exception type could return a Response with a 400 code and a custom validation error message.
T
- The exception type that the mapper can handleexceptionClass
- The type of exception to map.exceptionMapper
- A function that creates a Response
suitable for the exception.@Deprecated public RestHandlerBuilder withDocumentation()
withOpenApiJsonUrl(String)
and/or withOpenApiHtmlUrl(String)
public RestHandler build()
build
in interface MuHandlerBuilder<RestHandler>
public static RestHandlerBuilder restHandler(Object... resources)
Creates a handler builder for JAX-RS REST services.
Note that CORS is disabled by default.
resources
- Instances of classes that have a Path
annotation.public RestHandlerBuilder withCORS(CORSConfig corsConfig)
Specifies the CORS config for the REST services. Defaults to CORSConfigBuilder.disabled()
corsConfig
- The CORS config to useCORSConfigBuilder
public RestHandlerBuilder withCORS(CORSConfigBuilder corsConfig)
Specifies the CORS config for the REST services. Defaults to CORSConfigBuilder.disabled()
corsConfig
- The CORS config to useCORSConfigBuilder
@Deprecated public static RestHandler create(Object... resources)
resources
- Resources to registerpublic RestHandlerBuilder addRequestFilter(javax.ws.rs.container.ContainerRequestFilter filter)
Registers a request filter, which is run before a rest method is executed.
It will be run after the method has been matched, or if the PreMatching
annotation is applied to the
filter then it will run before matching occurs.
filter
- The filter to registerpublic RestHandlerBuilder addResponseFilter(javax.ws.rs.container.ContainerResponseFilter filter)
filter
- The filter to registerCopyright © 2017–2019. All rights reserved.