Return-Path: Delivered-To: apmail-cxf-users-archive@www.apache.org Received: (qmail 68425 invoked from network); 28 Apr 2009 16:30:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 28 Apr 2009 16:30:24 -0000 Received: (qmail 11713 invoked by uid 500); 28 Apr 2009 16:30:24 -0000 Delivered-To: apmail-cxf-users-archive@cxf.apache.org Received: (qmail 11654 invoked by uid 500); 28 Apr 2009 16:30:23 -0000 Mailing-List: contact users-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@cxf.apache.org Delivered-To: mailing list users@cxf.apache.org Received: (qmail 11644 invoked by uid 99); 28 Apr 2009 16:30:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Apr 2009 16:30:23 +0000 X-ASF-Spam-Status: No, hits=1.5 required=10.0 tests=SPF_PASS,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [192.77.186.17] (HELO mx3.progress.com) (192.77.186.17) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Apr 2009 16:30:14 +0000 Received: from mx3.progress.com (127.0.0.1) by mx3.progress.com (MlfMTA v3.2r9) id husnhc0171s2 for ; Tue, 28 Apr 2009 12:29:53 -0400 (envelope-from ) Received: from progress.com ([192.233.92.16]) by mx3.progress.com (SonicWALL 6.2.2.1073) with ESMTP; Tue, 28 Apr 2009 12:29:48 -0400 Received: from NTEXFE01.bedford.progress.com (ntexfe01 [10.128.10.24]) by progress.com (8.13.8/8.13.8) with ESMTP id n3SGTlhV011311 for ; Tue, 28 Apr 2009 12:29:47 -0400 (EDT) Received: from sberyoz ([10.5.2.18]) by NTEXFE01.bedford.progress.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Tue, 28 Apr 2009 12:29:46 -0400 Message-ID: <021801c9c81e$8ae64290$1202050a@emea.progress.com> From: "Sergey Beryozkin" To: References: <5e4e20250904201026h3db767d3hcf38a2e7dc34a2e8@mail.gmail.com> <059b01c9c406$c7636e80$1202050a@emea.progress.com> <5e4e20250904280905r194a5436hd648af81372d26a2@mail.gmail.com> Subject: Re: Error : No message body writer found for response class : ArrayList. - A String is OK... Date: Tue, 28 Apr 2009 17:29:44 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.3138 x-mimeole: Produced By Microsoft MimeOLE V6.00.2900.3350 X-OriginalArrivalTime: 28 Apr 2009 16:29:47.0224 (UTC) FILETIME=[8B873D80:01C9C81E] X-Mlf-Version: 6.2.2.1073 X-Mlf-UniqueId: o200904281629470220198 X-Virus-Checked: Checked by ClamAV on apache.org Hi Raphael It should work fine - but I forgot top tell you that I only added genericEntity support very recently, as part of working on the TCK compliance, sorry about it. CXF 2.2.1 has just been released - it should have this fix. I believe the only reason GenericEntity is there is that it allows users to write providers for parameterized types. It has the information about the raw type and the generic type, and the runtime uses this information to find a matching provider. By the way, in getSize() you just need to return -1, unless you actually know the (Content-Length) value. It's a hint to the runtime on how to set a Content-Length HTTP response header, if it's -1 then it will be up to the underlying HTTP container on how to set it in isWriteable() there's no need to check for media types, as you already set them in Produces(), but you might want to check that the genericType is String.class... cheers, Sergey ----- Original Message ----- From: "Raphael F." To: Sent: Tuesday, April 28, 2009 5:05 PM Subject: Re: Error : No message body writer found for response class : ArrayList. - A String is OK... Hi Serguey, Thanks for your help. I'm just back from short holidays. So I've created a class implementing MessageBodyWriter interface for it can accept List entity, as shown below : ----------------------------------------------------------- @Provider @Produces("text/xml,text/plain") public class StringListBodyWriter implements MessageBodyWriter> { public long getSize(List t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { Iterator i = t.iterator(); long size = 0; while (i.hasNext()) { size += i.next().length(); System.out.println("La taille de " + i + " est : " + i.next().length()); } return size; } public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return type.equals(List.class) && (mediaType.equals(MediaType.TEXT_PLAIN_TYPE) | mediaType .equals(MediaType.TEXT_XML_TYPE)); } public void writeTo(List t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( entityStream)); String ts = null; Iterator i = t.iterator(); while (i.hasNext()) { ts += i.next().toString(); System.out.println("La String tString est :\n" + ts); } bw.write(ts); bw.flush(); } } ----------------------------------------------------------- But, at runtime, I still have a similar error message : --------------------------- 28 avr. 2009 16:49:58 org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage ATTENTION: .No message body writer found for response class : ArrayList. --------------------------- So how could the StringListBodyWriter class could be used when the List based generic-entity Response is built ? I don't really see when the StringListBodyWriter class is called : --------------------------- return Response.ok(new GenericEntity>(results){}).entity(results).build(); --------------------------- Thanks, Raphael. 2009/4/23 Sergey Beryozkin : > Hi, > > One way is to register a message body writer for List which will check if > it contains String. It is somewhat primitive but very simple solution which > will also scale (as far as handling lists with various types is concerned) > quite well. > A more type safe way is to register a writer for List and then wrap > your list into a GenericEntity : > > List results = this.getX2dbiResults(fileContent); > return Response.ok( > new GenericEntity>(results)).build(); > > > > cheers, Sergey > > > ----- Original Message ----- From: "Raphael F." > To: > Sent: Monday, April 20, 2009 6:26 PM > Subject: Error : No message body writer found for response class : > ArrayList. - A String is OK... > > > Hello everibody; > > In my program, i send a file @ /postXML from a client class using > HttpClient and PostMethod objects. At server side, I have 2 String > objects to return (one with data queried, the second with debug data, > both are necessary) in a List object to the client but I have > a problem... Here is the server side code : > > [...] > @POST > @Path("/postXML") > public Response postXML(InputStream fileContent) { > > List results = this.getX2dbiResults(fileContent); > Response resp = Response.ok(results).build(); > > return resp; > } > [...] > > At client side, the code is : > > [...] > RequestEntity entity = new FileRequestEntity(input, "text/xml"); > PostMethod post = new PostMethod("http://localhost:9000/postXML"); > post.addRequestHeader("Accept", "text/xml"); > post.setRequestEntity(entity); > > HttpClient httpclient = new HttpClient(); > > try { > int result = httpclient.executeMethod(post); > System.out.println("Response status code: " + result); > System.out.println("Response body: "); > System.out.println(post.getResponseBodyAsString()); > } > finally { > post.releaseConnection(); > } > [...] > > When i execute client class, I get this message : > > Response status code: 500 > Response body: > .No message body writer found for response class : ArrayList. > > At server side, I have this information : > 20 avr. 2009 18:37:16 > org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor > writeResponseErrorMessage > .No message body writer found for response class : ArrayList. > > When I use a String for the result in Response.ok(results).build(), > there is no error, so how is it possible to return another entity than > String (i.e. an ArrayList) into Response.ok().build() ? > > Thanks for all.