Here what i has in mind:
public class MyClient {
private MyClient() {
// no-op
}
public static void main(final String[] args) throws JAXBException {
Client client = ClientBuilder.newClient();
try {
client.register(new
MyJaxbProvider<>(JAXBContext.newInstance(MyRoot.class)))
.target("http://foo.bar")
.request()
.get();
} finally {
client.close();
}
}
@XmlRootElement
public static class MyRoot {
// ...
}
// note: base impl, error handling + stream security to enhance etc
public static class MyJaxbProvider<T> implements
MessageBodyReader<T>, MessageBodyWriter<T> {
private final JAXBContext context;
public MyJaxbProvider(final JAXBContext context) {
this.context = context;
}
private boolean isJaxb(final Class<?> type) {
return type.isAnnotationPresent(XmlRootElement.class) ||
type.isAnnotationPresent(XmlType.class);
}
@Override
public boolean isReadable(final Class<?> type, final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {
return isJaxb(type);
}
@Override
public T readFrom(final Class<T> type, final Type genericType,
Annotation[] annotations,
final MediaType mediaType, final
MultivaluedMap<String, String> httpHeaders,
final InputStream entityStream)
throws WebApplicationException {
// customize the xml size etc if no other protection in
front - see AbsrtactJAXBProvider configureReader
try {
return
context.createUnmarshaller().unmarshal(StaxUtils.createXMLStreamReader(entityStream),
type).getValue();
} catch (final JAXBException e) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
}
@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {
return isJaxb(type);
}
@Override
public void writeTo(final T t, final Class<?> type, final Type
genericType,
final Annotation[] annotations, final
MediaType mediaType,
final MultivaluedMap<String, Object>
httpHeaders, final OutputStream entityStream)
throws WebApplicationException {
try {
context.createMarshaller().marshal(t,
StaxUtils.createXMLStreamWriter(entityStream));
} catch (final JAXBException e) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
}
}
}
Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> | Blog
<https://rmannibucau.metawerx.net/> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book
<https://www.packtpub.com/application-development/java-ee-8-high-performance>
Le lun. 29 oct. 2018 à 15:41, exabrial12 <[email protected]> a écrit :
> Romain Manni-Bucau wrote
> > You register you provider(s) and cxf should let it be taken cause user
> > providers are higher priority for the same media type.
>
> Got it, so create our own handlers for XML.
>
>
> Romain Manni-Bucau wrote
> > The messagebody reader/writer are trivial to impl if you have the
> context
> > so yes, just dont try to reuse cxf but just jaxrs here. Cxf logic is
> > mainly
> > grabbing the jaxbcontext so if you have it no nees of that cxf logic.
>
> Apologies, I'm not understanding what you're saying.
>
> Given this:
>
> https://github.com/apache/cxf/blob/master/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java#L479
>
> I would suspect the "correct" way to fix it would be to to simply put all
> of
> the Providers into the CXF MessageContext somehow?
>
> Or, if you can tell me the best place to do it, I'll see if I can put a
> patch together, just try to be as specific as possible. Thank you!
>
>
>
> --
> Sent from:
> http://tomee-openejb.979440.n4.nabble.com/TomEE-Users-f979441.html
>