You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sergey Beryozkin <se...@iona.com> on 2009/09/01 10:56:21 UTC

Re: No message body writer found for response class : String[]

Hi

What about isWriteable() code ? This code does not seem right :

type.equals(ArrayList.class) 

can you please change it to something like :
List.class.isAssignableFrom(type) ? 

Can you add a breakpoint to isWriteable ?

cheers, Sergey



mraible wrote:
> 
> I thought I replied to this a couple weeks ago, but apparently not. I'm
> still experiencing this error even after adding
> @Produces({"text/xml","text/plain","application/json"}).
> 
> Thanks,
> 
> Matt
> 
> 
> Sergey Beryozkin wrote:
>> 
>> Hi
>> 
>> CXF 2.2.3 only supports the arrays/collections of JAXB beans. I've added
>> a task to support the explicit collections/arrays of arbitrary (non-JAXB)
>> types. 
>> 
>> Now, as far as your provider is concerned, I think the problem is to do
>> with this declaration :
>> 
>> @Produces("text/xml,text/plain,application/json")
>> 
>> try this instead :
>> 
>> @Produces({"text/xml","text/plain","application/json"})
>> 
>> let me know if it works please...
>> 
>> cheers, Sergey
>> 
>> 
>> mraible wrote:
>>> 
>>> I'm getting this message when trying to return a String[] from a RESTful
>>> service. After searching the mailing list, it seems the best solution is
>>> to change my method to return a List<String> and create a
>>> StringListBodyWriter as a provider. However, after doing that, I'm still
>>> getting a similar error:
>>> 
>>> No message body writer found for response class : ArrayList
>>> 
>>> Here's my StringArrayBodyWriter:
>>> 
>>> @Provider
>>> @Produces("text/xml,text/plain,application/json")
>>> public class StringListBodyWriter implements
>>> MessageBodyWriter<List<String>> {
>>> 
>>> 	public long getSize(List<String> t, Class<?> type, Type genericType,
>>> 	                    Annotation[] annotations, MediaType mediaType) {
>>> 		Iterator<String> i = t.iterator();
>>> 		long size = 0;
>>> 		while (i.hasNext()) {
>>> 			size += i.next().length();
>>> 		}
>>> 		return size;
>>> 	}
>>> 
>>> 	public boolean isWriteable(Class<?> type, Type genericType,
>>> 	                           Annotation[] annotations, MediaType
>>> mediaType) {
>>> 		return type.equals(ArrayList.class)
>>> 				&& (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) |
>>> 				mediaType.equals(MediaType.TEXT_XML_TYPE) |
>>> 				mediaType.equals(MediaType.APPLICATION_JSON_TYPE));
>>> 	}
>>> 
>>> 	public void writeTo(List<String> t, Class<?> type, Type genericType,
>>> 	                    Annotation[] annotations, MediaType mediaType,
>>> 	                    MultivaluedMap<String, Object> httpHeaders,
>>> 	                    OutputStream entityStream) throws IOException,
>>> 			WebApplicationException {
>>> 		BufferedWriter bw = new BufferedWriter(new
>>> OutputStreamWriter(entityStream));
>>> 		String ts = null;
>>> 		for (String aT : t) {
>>> 			ts += aT;
>>> 		}
>>> 		bw.write(ts);
>>> 		bw.flush();
>>> 	}
>>> }
>>> 
>>> Registered in Spring:
>>> 
>>>     <bean id="stringListProvider"
>>> class="com.company.app.service.StringListBodyWriter"/>
>>> 
>>>         .....
>>> 
>>>     <jaxrs:server id="restServer" address="/rest/">
>>>         <jaxrs:serviceBeans>
>>>             ...
>>>         </jaxrs:serviceBeans>
>>>         <jaxrs:providers>
>>>             <ref bean="jsonProvider"/>
>>>             <ref bean="stringListProvider"/>	        
>>>         </jaxrs:providers>
>>> 
>>> Am I doing something wrong? I'm using CXF 2.2.3.
>>> 
>>> Thanks,
>>> 
>>> Matt
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/No-message-body-writer-found-for-response-class-%3A-String---tp25070046p25236729.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: No message body writer found for response class : ArrayList

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

MessageBodyWriter should be parameterized by
Collection<PublisherReport>...
Using Collection only will work in 2.2.3/2.2.4, Benson has done some
fixes on the trunk so Collection<PublisherReport> will be the best
option going forward...

That said, it is not the first time users have to deal with writing
custom providers for primitive types be serialized in JSON...If no JAXB
involved then may be Jackson would do well, but perhaps shipping a
simple provider dealing with primitive types would help indeed...

Sergey  

-----Original Message-----
From: Gabo Manuel [mailto:kmanuel@solegysystems.com] 
Sent: 26 October 2009 03:46
To: users@cxf.apache.org
Subject: Re: No message body writer found for response class : ArrayList

Hi,

Just a pair of questions, is the message subject the exception message 
encountered? And, is the MessageBodyWriter intentionally marked as List 
while the method response is Collection? I think making it the other way

around should set it straight.

Gabo

rconline wrote:
> Hi Guys, 
>
> Read all the posts on the thread. Maybe my question is stupid or maybe
i
> dont get the problem at all.
>
> Here' my web service
>
>         @WebMethod
> 	@GET
> 	@Produces("application/json")
> 	@Path("/summaryList/")
> 	Collection<PublisherReport> getSummaryList(String test);
>
> I want the method should return a list of json objects. The Object
> PublisherReport contains primitive/equiv object as member variables.
>
> Do i need to write a messagewriter as 
>
> public class ListWriter implements
MessageBodyWriter<List<PublisherReport>>
> {...
> }
>
> Or am i not getting the problem at all?
>   
>
------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.423 / Virus Database: 270.14.32/2459 - Release Date:
10/25/09 19:57:00
>
>   

Re: No message body writer found for response class : ArrayList

Posted by Gabo Manuel <km...@solegysystems.com>.
Hi,

Just a pair of questions, is the message subject the exception message 
encountered? And, is the MessageBodyWriter intentionally marked as List 
while the method response is Collection? I think making it the other way 
around should set it straight.

Gabo

rconline wrote:
> Hi Guys, 
>
> Read all the posts on the thread. Maybe my question is stupid or maybe i
> dont get the problem at all.
>
> Here' my web service
>
>         @WebMethod
> 	@GET
> 	@Produces("application/json")
> 	@Path("/summaryList/")
> 	Collection<PublisherReport> getSummaryList(String test);
>
> I want the method should return a list of json objects. The Object
> PublisherReport contains primitive/equiv object as member variables.
>
> Do i need to write a messagewriter as 
>
> public class ListWriter implements MessageBodyWriter<List<PublisherReport>>
> {...
> }
>
> Or am i not getting the problem at all?
>   
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.423 / Virus Database: 270.14.32/2459 - Release Date: 10/25/09 19:57:00
>
>   

Re: No message body writer found for response class : ArrayList

Posted by rconline <rc...@gmail.com>.
Hi Guys, 

Read all the posts on the thread. Maybe my question is stupid or maybe i
dont get the problem at all.

Here' my web service

        @WebMethod
	@GET
	@Produces("application/json")
	@Path("/summaryList/")
	Collection<PublisherReport> getSummaryList(String test);

I want the method should return a list of json objects. The Object
PublisherReport contains primitive/equiv object as member variables.

Do i need to write a messagewriter as 

public class ListWriter implements MessageBodyWriter<List<PublisherReport>>
{...
}

Or am i not getting the problem at all?
-- 
View this message in context: http://www.nabble.com/No-message-body-writer-found-for-response-class-%3A-String---tp25070046p26047209.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: No message body writer found for response class : String[]

Posted by mraible <ma...@raibledesigns.com>.
I think strings/string is acceptable. If users want a better representation
of their data, they can create an object to wrap their strings. I've done
this in my application and no longer need String[] support, but I can see
how it might be useful for others.

Matt


Sergey Beryozkin wrote:
> 
> No, there're no utilities (in CXF) for wrapping String values into
> corresponding name:value JSON sequences or xml elements.
> What sort of wrapper names would be acceptable for Strings ?
> is it 'string' ?
> 
> so List<String> can be then be serialized like :
> <strings>
> <string>foo</string>
> <string>foo</string>
> <strings>
> 
> or
> 
> {{"string":"foo"},{"string":"bar"}}
> 
> ?
> 
> cheers, Sergey
> 
> 
> mraible wrote:
>> 
>> That fixed it - thanks! 
>> 
>> Now I just need to figure the best way to write the output to JSON and
>> XML. Are there some utilities I can use or should I just construct a
>> StringBuffer with the necessary wrapper characters?
>> 
>> Thanks,
>> 
>> Matt
>> 
>> 
>> Sergey Beryozkin wrote:
>>> 
>>> Hi
>>> 
>>> What about isWriteable() code ? This code does not seem right :
>>> 
>>> type.equals(ArrayList.class) 
>>> 
>>> can you please change it to something like :
>>> List.class.isAssignableFrom(type) ? 
>>> 
>>> Can you add a breakpoint to isWriteable ?
>>> 
>>> cheers, Sergey
>>> 
>>> 
>>> 
>>> mraible wrote:
>>>> 
>>>> I thought I replied to this a couple weeks ago, but apparently not. I'm
>>>> still experiencing this error even after adding
>>>> @Produces({"text/xml","text/plain","application/json"}).
>>>> 
>>>> Thanks,
>>>> 
>>>> Matt
>>>> 
>>>> 
>>>> Sergey Beryozkin wrote:
>>>>> 
>>>>> Hi
>>>>> 
>>>>> CXF 2.2.3 only supports the arrays/collections of JAXB beans. I've
>>>>> added a task to support the explicit collections/arrays of arbitrary
>>>>> (non-JAXB) types. 
>>>>> 
>>>>> Now, as far as your provider is concerned, I think the problem is to
>>>>> do with this declaration :
>>>>> 
>>>>> @Produces("text/xml,text/plain,application/json")
>>>>> 
>>>>> try this instead :
>>>>> 
>>>>> @Produces({"text/xml","text/plain","application/json"})
>>>>> 
>>>>> let me know if it works please...
>>>>> 
>>>>> cheers, Sergey
>>>>> 
>>>>> 
>>>>> mraible wrote:
>>>>>> 
>>>>>> I'm getting this message when trying to return a String[] from a
>>>>>> RESTful service. After searching the mailing list, it seems the best
>>>>>> solution is to change my method to return a List<String> and create a
>>>>>> StringListBodyWriter as a provider. However, after doing that, I'm
>>>>>> still getting a similar error:
>>>>>> 
>>>>>> No message body writer found for response class : ArrayList
>>>>>> 
>>>>>> Here's my StringArrayBodyWriter:
>>>>>> 
>>>>>> @Provider
>>>>>> @Produces("text/xml,text/plain,application/json")
>>>>>> public class StringListBodyWriter implements
>>>>>> MessageBodyWriter<List<String>> {
>>>>>> 
>>>>>> 	public long getSize(List<String> t, Class<?> type, Type genericType,
>>>>>> 	                    Annotation[] annotations, MediaType mediaType) {
>>>>>> 		Iterator<String> i = t.iterator();
>>>>>> 		long size = 0;
>>>>>> 		while (i.hasNext()) {
>>>>>> 			size += i.next().length();
>>>>>> 		}
>>>>>> 		return size;
>>>>>> 	}
>>>>>> 
>>>>>> 	public boolean isWriteable(Class<?> type, Type genericType,
>>>>>> 	                           Annotation[] annotations, MediaType
>>>>>> mediaType) {
>>>>>> 		return type.equals(ArrayList.class)
>>>>>> 				&& (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) |
>>>>>> 				mediaType.equals(MediaType.TEXT_XML_TYPE) |
>>>>>> 				mediaType.equals(MediaType.APPLICATION_JSON_TYPE));
>>>>>> 	}
>>>>>> 
>>>>>> 	public void writeTo(List<String> t, Class<?> type, Type genericType,
>>>>>> 	                    Annotation[] annotations, MediaType mediaType,
>>>>>> 	                    MultivaluedMap<String, Object> httpHeaders,
>>>>>> 	                    OutputStream entityStream) throws IOException,
>>>>>> 			WebApplicationException {
>>>>>> 		BufferedWriter bw = new BufferedWriter(new
>>>>>> OutputStreamWriter(entityStream));
>>>>>> 		String ts = null;
>>>>>> 		for (String aT : t) {
>>>>>> 			ts += aT;
>>>>>> 		}
>>>>>> 		bw.write(ts);
>>>>>> 		bw.flush();
>>>>>> 	}
>>>>>> }
>>>>>> 
>>>>>> Registered in Spring:
>>>>>> 
>>>>>>     <bean id="stringListProvider"
>>>>>> class="com.company.app.service.StringListBodyWriter"/>
>>>>>> 
>>>>>>         .....
>>>>>> 
>>>>>>     <jaxrs:server id="restServer" address="/rest/">
>>>>>>         <jaxrs:serviceBeans>
>>>>>>             ...
>>>>>>         </jaxrs:serviceBeans>
>>>>>>         <jaxrs:providers>
>>>>>>             <ref bean="jsonProvider"/>
>>>>>>             <ref bean="stringListProvider"/>	        
>>>>>>         </jaxrs:providers>
>>>>>> 
>>>>>> Am I doing something wrong? I'm using CXF 2.2.3.
>>>>>> 
>>>>>> Thanks,
>>>>>> 
>>>>>> Matt
>>>>>> 
>>>>> 
>>>>> 
>>>> 
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/No-message-body-writer-found-for-response-class-%3A-String---tp25070046p25258484.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: No message body writer found for response class : String[]

Posted by Sergey Beryozkin <se...@iona.com>.
No, there're no utilities (in CXF) for wrapping String values into
corresponding name:value JSON sequences or xml elements.
What sort of wrapper names would be acceptable for Strings ?
is it 'string' ?

so List<String> can be then be serialized like :
<strings>
<string>foo</string>
<string>foo</string>
<strings>

or

{{"string":"foo"},{"string":"bar"}}

?

cheers, Sergey


mraible wrote:
> 
> That fixed it - thanks! 
> 
> Now I just need to figure the best way to write the output to JSON and
> XML. Are there some utilities I can use or should I just construct a
> StringBuffer with the necessary wrapper characters?
> 
> Thanks,
> 
> Matt
> 
> 
> Sergey Beryozkin wrote:
>> 
>> Hi
>> 
>> What about isWriteable() code ? This code does not seem right :
>> 
>> type.equals(ArrayList.class) 
>> 
>> can you please change it to something like :
>> List.class.isAssignableFrom(type) ? 
>> 
>> Can you add a breakpoint to isWriteable ?
>> 
>> cheers, Sergey
>> 
>> 
>> 
>> mraible wrote:
>>> 
>>> I thought I replied to this a couple weeks ago, but apparently not. I'm
>>> still experiencing this error even after adding
>>> @Produces({"text/xml","text/plain","application/json"}).
>>> 
>>> Thanks,
>>> 
>>> Matt
>>> 
>>> 
>>> Sergey Beryozkin wrote:
>>>> 
>>>> Hi
>>>> 
>>>> CXF 2.2.3 only supports the arrays/collections of JAXB beans. I've
>>>> added a task to support the explicit collections/arrays of arbitrary
>>>> (non-JAXB) types. 
>>>> 
>>>> Now, as far as your provider is concerned, I think the problem is to do
>>>> with this declaration :
>>>> 
>>>> @Produces("text/xml,text/plain,application/json")
>>>> 
>>>> try this instead :
>>>> 
>>>> @Produces({"text/xml","text/plain","application/json"})
>>>> 
>>>> let me know if it works please...
>>>> 
>>>> cheers, Sergey
>>>> 
>>>> 
>>>> mraible wrote:
>>>>> 
>>>>> I'm getting this message when trying to return a String[] from a
>>>>> RESTful service. After searching the mailing list, it seems the best
>>>>> solution is to change my method to return a List<String> and create a
>>>>> StringListBodyWriter as a provider. However, after doing that, I'm
>>>>> still getting a similar error:
>>>>> 
>>>>> No message body writer found for response class : ArrayList
>>>>> 
>>>>> Here's my StringArrayBodyWriter:
>>>>> 
>>>>> @Provider
>>>>> @Produces("text/xml,text/plain,application/json")
>>>>> public class StringListBodyWriter implements
>>>>> MessageBodyWriter<List<String>> {
>>>>> 
>>>>> 	public long getSize(List<String> t, Class<?> type, Type genericType,
>>>>> 	                    Annotation[] annotations, MediaType mediaType) {
>>>>> 		Iterator<String> i = t.iterator();
>>>>> 		long size = 0;
>>>>> 		while (i.hasNext()) {
>>>>> 			size += i.next().length();
>>>>> 		}
>>>>> 		return size;
>>>>> 	}
>>>>> 
>>>>> 	public boolean isWriteable(Class<?> type, Type genericType,
>>>>> 	                           Annotation[] annotations, MediaType
>>>>> mediaType) {
>>>>> 		return type.equals(ArrayList.class)
>>>>> 				&& (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) |
>>>>> 				mediaType.equals(MediaType.TEXT_XML_TYPE) |
>>>>> 				mediaType.equals(MediaType.APPLICATION_JSON_TYPE));
>>>>> 	}
>>>>> 
>>>>> 	public void writeTo(List<String> t, Class<?> type, Type genericType,
>>>>> 	                    Annotation[] annotations, MediaType mediaType,
>>>>> 	                    MultivaluedMap<String, Object> httpHeaders,
>>>>> 	                    OutputStream entityStream) throws IOException,
>>>>> 			WebApplicationException {
>>>>> 		BufferedWriter bw = new BufferedWriter(new
>>>>> OutputStreamWriter(entityStream));
>>>>> 		String ts = null;
>>>>> 		for (String aT : t) {
>>>>> 			ts += aT;
>>>>> 		}
>>>>> 		bw.write(ts);
>>>>> 		bw.flush();
>>>>> 	}
>>>>> }
>>>>> 
>>>>> Registered in Spring:
>>>>> 
>>>>>     <bean id="stringListProvider"
>>>>> class="com.company.app.service.StringListBodyWriter"/>
>>>>> 
>>>>>         .....
>>>>> 
>>>>>     <jaxrs:server id="restServer" address="/rest/">
>>>>>         <jaxrs:serviceBeans>
>>>>>             ...
>>>>>         </jaxrs:serviceBeans>
>>>>>         <jaxrs:providers>
>>>>>             <ref bean="jsonProvider"/>
>>>>>             <ref bean="stringListProvider"/>	        
>>>>>         </jaxrs:providers>
>>>>> 
>>>>> Am I doing something wrong? I'm using CXF 2.2.3.
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> Matt
>>>>> 
>>>> 
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/No-message-body-writer-found-for-response-class-%3A-String---tp25070046p25242617.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: No message body writer found for response class : String[]

Posted by mraible <ma...@raibledesigns.com>.
That fixed it - thanks! 

Now I just need to figure the best way to write the output to JSON and XML.
Are there some utilities I can use or should I just construct a StringBuffer
with the necessary wrapper characters?

Thanks,

Matt


Sergey Beryozkin wrote:
> 
> Hi
> 
> What about isWriteable() code ? This code does not seem right :
> 
> type.equals(ArrayList.class) 
> 
> can you please change it to something like :
> List.class.isAssignableFrom(type) ? 
> 
> Can you add a breakpoint to isWriteable ?
> 
> cheers, Sergey
> 
> 
> 
> mraible wrote:
>> 
>> I thought I replied to this a couple weeks ago, but apparently not. I'm
>> still experiencing this error even after adding
>> @Produces({"text/xml","text/plain","application/json"}).
>> 
>> Thanks,
>> 
>> Matt
>> 
>> 
>> Sergey Beryozkin wrote:
>>> 
>>> Hi
>>> 
>>> CXF 2.2.3 only supports the arrays/collections of JAXB beans. I've added
>>> a task to support the explicit collections/arrays of arbitrary
>>> (non-JAXB) types. 
>>> 
>>> Now, as far as your provider is concerned, I think the problem is to do
>>> with this declaration :
>>> 
>>> @Produces("text/xml,text/plain,application/json")
>>> 
>>> try this instead :
>>> 
>>> @Produces({"text/xml","text/plain","application/json"})
>>> 
>>> let me know if it works please...
>>> 
>>> cheers, Sergey
>>> 
>>> 
>>> mraible wrote:
>>>> 
>>>> I'm getting this message when trying to return a String[] from a
>>>> RESTful service. After searching the mailing list, it seems the best
>>>> solution is to change my method to return a List<String> and create a
>>>> StringListBodyWriter as a provider. However, after doing that, I'm
>>>> still getting a similar error:
>>>> 
>>>> No message body writer found for response class : ArrayList
>>>> 
>>>> Here's my StringArrayBodyWriter:
>>>> 
>>>> @Provider
>>>> @Produces("text/xml,text/plain,application/json")
>>>> public class StringListBodyWriter implements
>>>> MessageBodyWriter<List<String>> {
>>>> 
>>>> 	public long getSize(List<String> t, Class<?> type, Type genericType,
>>>> 	                    Annotation[] annotations, MediaType mediaType) {
>>>> 		Iterator<String> i = t.iterator();
>>>> 		long size = 0;
>>>> 		while (i.hasNext()) {
>>>> 			size += i.next().length();
>>>> 		}
>>>> 		return size;
>>>> 	}
>>>> 
>>>> 	public boolean isWriteable(Class<?> type, Type genericType,
>>>> 	                           Annotation[] annotations, MediaType
>>>> mediaType) {
>>>> 		return type.equals(ArrayList.class)
>>>> 				&& (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) |
>>>> 				mediaType.equals(MediaType.TEXT_XML_TYPE) |
>>>> 				mediaType.equals(MediaType.APPLICATION_JSON_TYPE));
>>>> 	}
>>>> 
>>>> 	public void writeTo(List<String> t, Class<?> type, Type genericType,
>>>> 	                    Annotation[] annotations, MediaType mediaType,
>>>> 	                    MultivaluedMap<String, Object> httpHeaders,
>>>> 	                    OutputStream entityStream) throws IOException,
>>>> 			WebApplicationException {
>>>> 		BufferedWriter bw = new BufferedWriter(new
>>>> OutputStreamWriter(entityStream));
>>>> 		String ts = null;
>>>> 		for (String aT : t) {
>>>> 			ts += aT;
>>>> 		}
>>>> 		bw.write(ts);
>>>> 		bw.flush();
>>>> 	}
>>>> }
>>>> 
>>>> Registered in Spring:
>>>> 
>>>>     <bean id="stringListProvider"
>>>> class="com.company.app.service.StringListBodyWriter"/>
>>>> 
>>>>         .....
>>>> 
>>>>     <jaxrs:server id="restServer" address="/rest/">
>>>>         <jaxrs:serviceBeans>
>>>>             ...
>>>>         </jaxrs:serviceBeans>
>>>>         <jaxrs:providers>
>>>>             <ref bean="jsonProvider"/>
>>>>             <ref bean="stringListProvider"/>	        
>>>>         </jaxrs:providers>
>>>> 
>>>> Am I doing something wrong? I'm using CXF 2.2.3.
>>>> 
>>>> Thanks,
>>>> 
>>>> Matt
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/No-message-body-writer-found-for-response-class-%3A-String---tp25070046p25241946.html
Sent from the cxf-user mailing list archive at Nabble.com.