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/12/03 21:46:35 UTC

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

This is probably because the XML document is broken, you output multiple
fragments but the resulting doc has no root element.

So in your custom provider just write directly into the stream something
like
out.write("<files>") 

// write individual files here

out.write("</files>")

Alternatively rather than doing it all yourself just return
List<JAXBElement> with JAXBElements being initialized from the files. In
this case You'll need to configure JAXBElementProvider to wrap the
collection.
Or just return a List<InputStream> rather than reading into XML and then
writing into output stream.

cheers, Sergey



Parimal Dhinoja wrote:
> 
> Hi,
> 
> As you see in following code, My requirement is to send one or more XML
> files in response stream. I have done following to accomplish my
> requirement:
> 
> 1. Resource class method returns ArrayList which contains XML file
> objects.
> 2. I have created custom MessageBodyWriter, which modify the response, by
> getting files from Arraylist and write it into Response Stream.
> 
> I am accessing my service from Browser with url :
> http://localhost:443/application/service?x=fileDir
> 
> I have checked that I have got all the files in response body with 200Ok
> code, but browser shows XML parsing error as shown below.
> 
> "XML Parsing Error: junk after document element"
> 
> so is it problem from CXF side or it is browser error? any idea? Please
> note
> that if I send single xml file with same code, it returns successfully and
> browser display that file properly. I guess that because there are
> multiple
> files one after another, browser is not able to parsing it? is that true?
> 
> Please note, I need to send multiple files in response stream as it is
> third
> party client requirement.
> 
> 
> Regards,
> Parimal
> 
> 
> On Tue, Dec 1, 2009 at 2:09 PM, Parimal Dhinoja <pd...@gmail.com>
> wrote:
> 
>> Hi,
>>
>> I am getting error that my custom MessageBodyWriter  is not found.
>> following is my code. Please let me know what went wrong.
>>
>> My customMessageBodyWriter class
>>
>> @Provider
>> @Produces("text/xml")
>> public class CustomResBodyWriter implements
>> MessageBodyWriter<ArrayList>
>> {
>>     public static final int ARRAY_SIZE = 5000;
>>     private static final Logger LOGGER =
>> Logger.getLogger(CustomResBodyWriter.class);
>>
>>     @Override
>>     public long getSize(ArrayList arg0, Class<?> arg1,
>> java.lang.reflect.Type arg2,
>>                         Annotation[] arg3, MediaType arg4)
>>     {
>>          return arg0.size();
>>     }
>>
>>     @Override
>>     public boolean isWriteable(Class<?> arg0, java.lang.reflect.Type
>> arg1,
>> Annotation[] arg2,
>>                                MediaType arg3)
>>     {
>>            return ArrayList.class.isAssignableFrom(arg0);
>>     }
>>
>>     @Override
>>     public void writeTo(ArrayList arg0, Class<?> arg1,
>> java.lang.reflect.Type arg2,
>>                         Annotation[] arg3, MediaType arg4,
>> MultivaluedMap<String, Object> arg5,
>>                         OutputStream arg6) throws IOException,
>> WebApplicationException
>>     {
>>         BufferedWriter bw = new BufferedWriter(new
>> OutputStreamWriter(arg6));
>>         byte[] buffer = new byte[ARRAY_SIZE];
>>         String ts = null;
>>         int listSize = arg0.size();
>>         FileInputStream fis = null;
>>         for (int i = 0; i < listSize; i++)
>>         {
>>             File vFile = (File) arg0.get(i);
>>             fis = new FileInputStream(vFile);
>>             BufferedInputStream bufferedInputStream = new
>> BufferedInputStream(fis);
>>             while (true)
>>             {
>>                 int vBytesRead = bufferedInputStream.read(buffer, 0,
>> buffer.length);
>>                 if (vBytesRead < 0)
>>                 {
>>                     break;
>>                 }
>>                 arg6.write(buffer, 0, vBytesRead);
>>             }
>>             fis.close();
>>             bufferedInputStream.close();
>>             vFile = null;
>>         }
>>         arg6.flush();
>>
>>     }
>> }
>>
>> my configuration.
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <beans xmlns="http://www.springframework.org/schema/beans"
>>   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>   xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>>   xmlns:sec="http://cxf.apache.org/configuration/security"
>>   xmlns:http="http://cxf.apache.org/transports/http/configuration"
>>   xmlns:cxf="http://cxf.apache.org/core"
>>   xsi:schemaLocation="
>> http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans.xsd
>> http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
>> http://cxf.apache.org/transports/http/configuration
>> http://cxf.apache.org/schemas/configuration/http-conf.xsd
>> http://cxf.apache.org/configuration/security
>> http://cxf.apache.org/schemas/configuration/security.xsd
>> http://cxf.apache.org/core
>> http://cxf.apache.org/schemas/core.xsd">
>>
>>   <!-- do not use import statements if CXFServlet init parameters link to
>> this beans.xml -->
>>
>>   <import resource="classpath:META-INF/cxf/cxf.xml" />
>>   <import
>> resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"
>> />
>>   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>>
>>  <bean id="mapProvider"
>> class="com.traveltripper.stargazer.service.pms.opera.CustomResBodyWriter"/>
>>
>>   <jaxrs:server id="messageservice" address="/">
>> <jaxrs:features>
>>      <cxf:logging/>
>> </jaxrs:features>
>>     <jaxrs:serviceBeans>
>>       <ref bean="messageBean" />
>>     </jaxrs:serviceBeans>
>>      <jaxrs:providers>
>>       <ref bean="mapProvider" />
>>      </jaxrs:providers>
>>   </jaxrs:server>
>>
>>   <bean id="messageBean"
>> class="com.traveltripper.stargazer.service.pms.opera.ReservationXMLService"
>> />
>> </beans>
>>
>> my service implementation.
>>
>>  @GET
>>     @Path("/message5")
>>     @Produces("application/xml")
>>     public ArrayList getMessage5(@QueryParam("propertyName")
>>     String propertyName)
>>     {
>>         ArrayList fileList = new ArrayList();
>>         try
>>         {
>>             LOGGER.info("propertyName in Request :" + propertyName);
>>
>>             String filePath = "c://";
>>             LOGGER.info("FilePath=" + filePath);
>>             File vDirpath = new File(filePath);
>>             ArrayList<String> vProperties = new ArrayList<String>();
>>
>>             String dirname[] = vDirpath.list();
>>             for (int i = 0; i < dirname.length; i++)
>>             {
>>                 vProperties.add(dirname[i]);
>>                 LOGGER.info(dirname[i]);
>>             }
>>
>>             if (vProperties.contains(propertyName))
>>             {
>>
>>                 filePath = filePath + propertyName + "/";
>>                 File propertyPath = new File(filePath);
>>                 File[] resFiles = propertyPath.listFiles();
>>
>>                 for (int i = 0; i < resFiles.length; i++)
>>                 {
>>                     fileList.add(resFiles[i]);
>>
>>                 }
>>
>>             }
>>         }
>>         catch (Exception e)
>>         {
>>             LOGGER.info(e.getMessage());
>>         }
>>         return fileList;
>>
>>     }
>>
>>
>> --
>> Regards,
>> Parimal
>> "Nothing is stationary,Change is a part of Life"
>>
> 
> 
> 
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 
> 

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