Class RestHandlerBuilder
- java.lang.Object
-
- io.muserver.rest.RestHandlerBuilder
-
- All Implemented Interfaces:
MuHandlerBuilder<RestHandler>
public class RestHandlerBuilder extends java.lang.Object implements MuHandlerBuilder<RestHandler>
Used to create aRestHandler
for handling JAX-RS REST resources.- See Also:
restHandler(Object...)
-
-
Constructor Summary
Constructors Constructor Description RestHandlerBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <P> RestHandlerBuilder
addCustomParamConverter(java.lang.Class<P> paramClass, jakarta.ws.rs.ext.ParamConverter<P> converter)
Registers a parameter converter class that convert strings to and from a custom class.RestHandlerBuilder
addCustomParamConverterProvider(jakarta.ws.rs.ext.ParamConverterProvider paramConverterProvider)
Registers an object that can convert rest method parameters (e.g.<T> RestHandlerBuilder
addCustomReader(jakarta.ws.rs.ext.MessageBodyReader<T> reader)
Registers an object that can deserialise request bodies into custom classes.RestHandlerBuilder
addCustomSchema(java.lang.Class<?> dataClass, SchemaObject schema)
Registers a custom OpenAPI schema description for the given class.<T> RestHandlerBuilder
addCustomWriter(jakarta.ws.rs.ext.MessageBodyWriter<T> writer)
Registers an object that can write custom classes to responses.<T extends java.lang.Throwable>
RestHandlerBuilderaddExceptionMapper(java.lang.Class<T> exceptionClass, jakarta.ws.rs.ext.ExceptionMapper<T> exceptionMapper)
Adds a mapper that converts an exception to a response.RestHandlerBuilder
addReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor readerInterceptor)
Registers a reader interceptor allowing for inspection and alteration of request bodies.RestHandlerBuilder
addRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter filter)
Registers a request filter, which is run before a rest method is executed.RestHandlerBuilder
addResource(java.lang.Object... resources)
Adds one or more rest resources to this handlerRestHandlerBuilder
addResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter filter)
Registers a response filter, which is called after execution of a method takes place.RestHandlerBuilder
addSchemaObjectCustomizer(SchemaObjectCustomizer customizer)
Registers an object that is able to customizeSchemaObject
s generated by this rest handler for OpenAPI documentation.RestHandlerBuilder
addWriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor writerInterceptor)
Registers a writer interceptor allowing for inspection and alteration of response bodies.RestHandler
build()
CollectionParameterStrategy
collectionParameterStrategy()
CORSConfig
corsConfig()
java.util.List<jakarta.ws.rs.ext.ParamConverterProvider>
customParamConverterProviders()
java.util.List<jakarta.ws.rs.ext.MessageBodyReader<?>>
customReaders()
java.util.Map<java.lang.Class<?>,SchemaObject>
customSchemas()
java.util.List<jakarta.ws.rs.ext.MessageBodyWriter<?>>
customWriters()
java.util.Map<java.lang.Class<? extends java.lang.Throwable>,jakarta.ws.rs.ext.ExceptionMapper<? extends java.lang.Throwable>>
exceptionMappers()
java.lang.String
openApiHtmlCss()
java.lang.String
openApiHtmlUrl()
java.lang.String
openApiJsonUrl()
OpenAPIObjectBuilder
openAPIObject()
java.util.List<jakarta.ws.rs.container.ContainerRequestFilter>
preMatchRequestFilters()
java.util.List<jakarta.ws.rs.ext.ReaderInterceptor>
readerInterceptors()
java.util.List<jakarta.ws.rs.container.ContainerRequestFilter>
requestFilters()
java.util.List<java.lang.Object>
resources()
java.util.List<jakarta.ws.rs.container.ContainerResponseFilter>
responseFilters()
static RestHandlerBuilder
restHandler(java.lang.Object... resources)
Creates a handler builder for JAX-RS REST services.java.util.List<SchemaObjectCustomizer>
schemaObjectCustomizers()
RestHandlerBuilder
withCollectionParameterStrategy(CollectionParameterStrategy collectionParameterStrategy)
Specifies if values passed to method parameters withQueryParam
orHeaderParam
annotations should be transformed or not.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
withOpenApiDocument(OpenAPIObjectBuilder openAPIObject)
Use this value to create JSON and HTML documentation for your rest service.RestHandlerBuilder
withOpenApiHtmlCss(java.lang.String css)
When using the HTML endpoint made available by callingwithOpenApiDocument(OpenAPIObjectBuilder)
this allows you to override the default CSS that is used.RestHandlerBuilder
withOpenApiHtmlUrl(java.lang.String url)
Enables a simple HTML endpoint that documents the API exposed by the rest resources declared by this builder.RestHandlerBuilder
withOpenApiJsonUrl(java.lang.String url)
Enables an Open API JSON URL at the specified endpoint.java.util.List<jakarta.ws.rs.ext.WriterInterceptor>
writerInterceptors()
-
-
-
Method Detail
-
addResource
public RestHandlerBuilder addResource(java.lang.Object... resources)
Adds one or more rest resources to this handler- Parameters:
resources
- One or more instances of classes that are decorated withPath
annotations.- Returns:
- This builder
-
addCustomWriter
public <T> RestHandlerBuilder addCustomWriter(jakarta.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 aMessageBodyWriter<MyClass>
writer.- Type Parameters:
T
- The type of object that the writer can serialise- Parameters:
writer
- A response body writer- Returns:
- This builder
-
addCustomReader
public <T> RestHandlerBuilder addCustomReader(jakarta.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 aMessageBodyReader<MyClass>
reader.- Type Parameters:
T
- The type of object that the reader can deserialise- Parameters:
reader
- A request body reader- Returns:
- This builder
-
addCustomParamConverterProvider
public RestHandlerBuilder addCustomParamConverterProvider(jakarta.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)
- Parameters:
paramConverterProvider
- A provider of parameter converters- Returns:
- This builder
-
addCustomParamConverter
public <P> RestHandlerBuilder addCustomParamConverter(java.lang.Class<P> paramClass, jakarta.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.- Type Parameters:
P
- The type of the parameter- Parameters:
paramClass
- The class that this converter is meant for.converter
- The converter- Returns:
- This builder
-
withOpenApiJsonUrl
public RestHandlerBuilder withOpenApiJsonUrl(java.lang.String url)
Enables an Open API JSON URL at the specified endpoint. This JSON describes the API exposed by the rest resources declared by this builder, and can be used by UIs such as Swagger.- Parameters:
url
- The URL to serve from, for example/openapi.json
ornull
to disable the JSON endpoint. Disabled by default.- Returns:
- The current Rest Handler Builder
- See Also:
withOpenApiDocument(OpenAPIObjectBuilder)
,withOpenApiHtmlUrl(String)
-
withOpenApiHtmlUrl
public RestHandlerBuilder withOpenApiHtmlUrl(java.lang.String url)
Enables a simple HTML endpoint that documents the API exposed by the rest resources declared by this builder.- Parameters:
url
- The URL to serve from, for example/api.html
ornull
to disable the HTML endpoint. Disabled by default.- Returns:
- The current Rest Handler Builder
- See Also:
withOpenApiDocument(OpenAPIObjectBuilder)
,withOpenApiJsonUrl(String)
,withOpenApiHtmlCss(String)
-
withCollectionParameterStrategy
public RestHandlerBuilder withCollectionParameterStrategy(CollectionParameterStrategy collectionParameterStrategy)
Specifies if values passed to method parameters withQueryParam
orHeaderParam
annotations should be transformed or not.The primary use of this is to allow querystring parameters such as
/path?value=one,two,three
to be interpreted as a list of three values rather than a single string. This only applies to parameters that are collections.The default is
CollectionParameterStrategy.NO_TRANSFORM
which is the JAX-RS standard.Note: until MuServer 1.0, if no value is specified but methods with collection parameters are detected then the handler will fail to start and this value will need to be explicitly set. This is in order to highlight the change in behaviour introduced in Mu Server 0.70 where it used
CollectionParameterStrategy.SPLIT_ON_COMMA
behaviour.- Parameters:
collectionParameterStrategy
- The strategy to use- Returns:
- This builder
-
withOpenApiHtmlCss
public RestHandlerBuilder withOpenApiHtmlCss(java.lang.String css)
When using the HTML endpoint made available by callingwithOpenApiDocument(OpenAPIObjectBuilder)
this allows you to override the default CSS that is used.- Parameters:
css
- A string containing a style sheet definition.- Returns:
- The current Rest Handler Builder
-
withOpenApiDocument
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
- Parameters:
openAPIObject
- An API Object builder with theOpenAPIObjectBuilder.withInfo(InfoObject)
set.- Returns:
- The current Rest Handler Builder
- See Also:
OpenAPIObjectBuilder.openAPIObject()
,withOpenApiJsonUrl(String)
,withOpenApiHtmlUrl(String)
-
addExceptionMapper
public <T extends java.lang.Throwable> RestHandlerBuilder addExceptionMapper(java.lang.Class<T> exceptionClass, jakarta.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.
- Type Parameters:
T
- The exception type that the mapper can handle- Parameters:
exceptionClass
- The type of exception to map.exceptionMapper
- A function that creates aResponse
suitable for the exception.- Returns:
- Returns this builder.
-
addSchemaObjectCustomizer
public RestHandlerBuilder addSchemaObjectCustomizer(SchemaObjectCustomizer customizer)
Registers an object that is able to customizeSchemaObject
s generated by this rest handler for OpenAPI documentation.This is only used when calling the URL specified by
withOpenApiJsonUrl(String)
Note: if a rest resource implements
SchemaObjectCustomizer
then it will be automatically registered.- Parameters:
customizer
- The customizer to register- Returns:
- This builder
-
restHandler
public static RestHandlerBuilder restHandler(java.lang.Object... resources)
Creates a handler builder for JAX-RS REST services.
Note that CORS is disabled by default.
- Parameters:
resources
- Instances of classes that have aPath
annotation.- Returns:
- Returns a builder that can be used to specify more config
-
withCORS
public RestHandlerBuilder withCORS(CORSConfig corsConfig)
Specifies the CORS config for the REST services. Defaults to
CORSConfigBuilder.disabled()
Note: an alternative to adding CORS config to the Rest Handler Builder is to add a handler with
CORSHandlerBuilder.corsHandler()
which can apply the headers to all handlers (not just JAX-RS endpoints).- Parameters:
corsConfig
- The CORS config to use- Returns:
- This builder.
- See Also:
CORSConfigBuilder
-
withCORS
public RestHandlerBuilder withCORS(CORSConfigBuilder corsConfig)
Specifies the CORS config for the REST services. Defaults to
CORSConfigBuilder.disabled()
Note: an alternative to adding CORS config to the Rest Handler Builder is to add a handler with
CORSHandlerBuilder.corsHandler()
which can apply the headers to all handlers (not just JAX-RS endpoints).- Parameters:
corsConfig
- The CORS config to use- Returns:
- This builder.
- See Also:
CORSConfigBuilder
-
addRequestFilter
public RestHandlerBuilder addRequestFilter(jakarta.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.To access the
ResourceInfo
orMuRequest
for the current request, the following code can be used:ResourceInfo resourceInfo = (ResourceInfo) context.getProperty(MuRuntimeDelegate.RESOURCE_INFO_PROPERTY); MuRequest muRequest = (MuRequest) context.getProperty(MuRuntimeDelegate.MU_REQUEST_PROPERTY);
- Parameters:
filter
- The filter to register- Returns:
- This builder
-
addResponseFilter
public RestHandlerBuilder addResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter filter)
Registers a response filter, which is called after execution of a method takes place.To access the
ResourceInfo
orMuRequest
for the current request, the following code can be used:ResourceInfo resourceInfo = (ResourceInfo) context.getProperty(MuRuntimeDelegate.RESOURCE_INFO_PROPERTY); MuRequest muRequest = (MuRequest) context.getProperty(MuRuntimeDelegate.MU_REQUEST_PROPERTY);
- Parameters:
filter
- The filter to register- Returns:
- This builder
-
addCustomSchema
public RestHandlerBuilder addCustomSchema(java.lang.Class<?> dataClass, SchemaObject schema)
Registers a custom OpenAPI schema description for the given class.This allows you to provide rich schema objects (created with
SchemaObjectBuilder.schemaObject()
) in your OpenAPI documents. Wherever the give type is used as a parameter or body, the given schema will be used to describe it.Warning: When generating OpenAPI documentation, the schema information will be added to the
/components/schemas
section with a key equal to the simple class name of the given data class. If you do not wish to expose the class name in your API documentation, you can override it by annotating the class with aDescription
annotation in which case thevalue
field will be used.- Parameters:
dataClass
- The type of class to describeschema
- The schema object describing the class- Returns:
- This builder
-
addWriterInterceptor
public RestHandlerBuilder addWriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor writerInterceptor)
Registers a writer interceptor allowing for inspection and alteration of response bodies.Interceptors are executed in the order added, and are called before any message body writers added by
addCustomWriter(MessageBodyWriter)
.To access the
ResourceInfo
orMuRequest
for the current request, the following code can be used:ResourceInfo resourceInfo = (ResourceInfo) context.getProperty(MuRuntimeDelegate.RESOURCE_INFO_PROPERTY); MuRequest muRequest = (MuRequest) context.getProperty(MuRuntimeDelegate.MU_REQUEST_PROPERTY);
- Parameters:
writerInterceptor
- The interceptor to add. Ifnull
then this is a no-op.- Returns:
- This builder
-
addReaderInterceptor
public RestHandlerBuilder addReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor readerInterceptor)
Registers a reader interceptor allowing for inspection and alteration of request bodies.Interceptors are executed in the order added, and are called before any message body readers added by
addCustomReader(MessageBodyReader)
.To access the
ResourceInfo
orMuRequest
for the current request, the following code can be used:ResourceInfo resourceInfo = (ResourceInfo) context.getProperty(MuRuntimeDelegate.RESOURCE_INFO_PROPERTY); MuRequest muRequest = (MuRequest) context.getProperty(MuRuntimeDelegate.MU_REQUEST_PROPERTY);
- Parameters:
readerInterceptor
- The interceptor to add. Ifnull
then this is a no-op.- Returns:
- This builder
-
resources
public java.util.List<java.lang.Object> resources()
- Returns:
- The current value of this property
-
customWriters
public java.util.List<jakarta.ws.rs.ext.MessageBodyWriter<?>> customWriters()
- Returns:
- The current value of this property
-
writerInterceptors
public java.util.List<jakarta.ws.rs.ext.WriterInterceptor> writerInterceptors()
- Returns:
- The current value of this property
-
customReaders
public java.util.List<jakarta.ws.rs.ext.MessageBodyReader<?>> customReaders()
- Returns:
- The current value of this property
-
readerInterceptors
public java.util.List<jakarta.ws.rs.ext.ReaderInterceptor> readerInterceptors()
- Returns:
- The current value of this property
-
customParamConverterProviders
public java.util.List<jakarta.ws.rs.ext.ParamConverterProvider> customParamConverterProviders()
- Returns:
- The current value of this property
-
customSchemas
public java.util.Map<java.lang.Class<?>,SchemaObject> customSchemas()
- Returns:
- The current value of this property
-
openApiJsonUrl
public java.lang.String openApiJsonUrl()
- Returns:
- The current value of this property
-
openApiHtmlUrl
public java.lang.String openApiHtmlUrl()
- Returns:
- The current value of this property
-
openAPIObject
public OpenAPIObjectBuilder openAPIObject()
- Returns:
- The current value of this property
-
openApiHtmlCss
public java.lang.String openApiHtmlCss()
- Returns:
- The current value of this property
-
exceptionMappers
public java.util.Map<java.lang.Class<? extends java.lang.Throwable>,jakarta.ws.rs.ext.ExceptionMapper<? extends java.lang.Throwable>> exceptionMappers()
- Returns:
- The current value of this property
-
preMatchRequestFilters
public java.util.List<jakarta.ws.rs.container.ContainerRequestFilter> preMatchRequestFilters()
- Returns:
- The current value of this property
-
requestFilters
public java.util.List<jakarta.ws.rs.container.ContainerRequestFilter> requestFilters()
- Returns:
- The current value of this property
-
responseFilters
public java.util.List<jakarta.ws.rs.container.ContainerResponseFilter> responseFilters()
- Returns:
- The current value of this property
-
corsConfig
public CORSConfig corsConfig()
- Returns:
- The current value of this property
-
schemaObjectCustomizers
public java.util.List<SchemaObjectCustomizer> schemaObjectCustomizers()
- Returns:
- The current value of this property
-
collectionParameterStrategy
public CollectionParameterStrategy collectionParameterStrategy()
- Returns:
- The current value of this property
-
build
public RestHandler build()
- Specified by:
build
in interfaceMuHandlerBuilder<RestHandler>
- Returns:
- The newly build
RestHandler
-
-