[
https://issues.apache.org/jira/browse/CXF-6796?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15158721#comment-15158721
]
Sergey Beryozkin edited comment on CXF-6796 at 2/23/16 10:52 AM:
-----------------------------------------------------------------
Hi Jim,
If we relax StringTextProvider to accept Object then it will start competing
with PrimtiveTextProvider which handles Boolean/Number/etc such that a spec
requirement (that Boolean/Number/etc are only supported OOB if it is
text/plain) will be broken if it is say 'text/html', etc.
For example, suppose we have a client with Accept: texthtml and the method
returns Boolean, and if StringTextProvider accepts object then it will be
chosen and return Boolean.
In fact it will convert Order to String even it is Accept: application/json if
for some reasons the default Jettison or Jackson won't be able to write it and
that would be a problem.
The easiest thing to do is simply to create a custom text/plain
MessageBodyWriter that will convert arbitrary objects to String. The other
option is to write ContainerResponseFilter which will convert the response body
to String and then will reset it on the response container context - so that
the service code does not convert to String
Cheers, Sergey
was (Author: sergey_beryozkin):
Hi Jim,
If we relax StringTextProvider to accept Object then it will start competing
with PrimtiveTextProvider which handles Boolean/Number/etc such that a spec
requirement (that Boolean/Number/etc are only supported OOB if it is
text/plain) will be broken if it is say 'text/html', etc.
For example, suppose we have a client with Accept: texthtml and the method
returns Boolean, and if StringTextProvider accepts object then it will be
chosen and return Boolean.
In fact it will convert Order to String even it is Accept: application/json if
for some reasons the default Jettison or Jackson won't be able to write it and
that would be a problem.
The easiest thing to do is simply to create a custom text/plain
MessageBodyWriter that will convert arbitrary objects to String. The other
option is to write ContainerResponseFilter which will convert the response body
to String and then will reset it on the response container context - so that
the client code does not convert to String
Cheers, Sergey
> Improve or extend StringTextProvider to write an Object
> --------------------------------------------------------
>
> Key: CXF-6796
> URL: https://issues.apache.org/jira/browse/CXF-6796
> Project: CXF
> Issue Type: Improvement
> Components: JAX-RS
> Affects Versions: 3.1.5
> Reporter: Jim Ma
> Fix For: 3.1.6
>
>
> CXF reports it can't find the message writer for OrderModel class and
> text/plain content type when the resource class like the following:
> {code}
> @Produces("text/plain")
> @Path("order/{id}")
> public class OrderResource {
> @GET
> public OrderModel get(@PathParam("id") @CustomMax int id) {
> return new OrderModel(id);
> }
> }
> {code}
> It is required to change the return Class to String and explicitly invoke
> toString() return the String value
> {code}.
> @Produces("text/plain")
> @Path("order/{id}")
> public class OrderResource {
> @GET
> public String get(@PathParam("id") @CustomMax int id) {
> return new OrderModel(id).toString();
> }
> }
> {code}
> We can improve StringTextProvider to write the Object with toString value to
> remove the return type must be String class restriction.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)