Java Reference
In-Depth Information
<
T
extends
extends
Throwable
>
ExceptionMapper
<
T
>
getExceptionMapper
(
Class
<
T
>
type
);
}
We use the
Providers.getContextResolver()
method to find a
ContextResolver
. We in-
ject a reference to a
Providers
object using the
@Context
annotation. Let's modify our
JAXBMarshaller
class to add this new functionality:
@Context
protected
Providers providers
;
public
public
void
void
writeTo
(
Object target
,
Class
<?>
type
,
Type genericType
,
Annotation
[]
annotations
,
MediaType mediaType
,
MultivaluedMap
<
String
,
Object
>
httpHeaders
,
OutputStream outputStream
)
throws
throws
IOException
{
try
try
{
JAXBContext ctx
=
null
null
;
ContextResolver
<
JAXBContext
>
resolver
=
providers
.
getContextResolver
(
JAXBContext
.
class
,
mediaType
);
iif
(
resolver
!=
null
null
) {
ctx
=
resolver
.
getContext
(
type
);
}
iif
(
ctx
==
null
null
) {
// create one ourselves
ctx
=
JAXBContext
.
newInstance
(
type
);
}
ctx
.
createMarshaller
().
marshal
(
target
,
outputStream
);
}
catch
catch
(
JAXBException ex
) {
throw
throw new
new
RuntimeException
(
ex
);
}
}
In our
writeTo()
method, we now use the
Providers
interface to find a
ContextResolver
that can give us a custom
JAXBContext
. If one exists, we call
resolver.getContext()
,
passing in the type of the object we want a
JAXBContext
for.
The
ContextResolver
returned by
Providers.getContextResolver()
is actually a proxy
that sits in front of a list of
ContextResolvers
that can provide
JAXBContext
instances.
When
getContextResolver()
is invoked, the proxy iterates on this list, recalling
getCon-