Hi

It is bizarre indeed, but I'm not quite sure if/how I can help. One possible
reason is that you write the start of the document using STAX writer but
then marshall the individual nodes directly into output stream.

Perhaps you might want to try avoiding doing the marshalling code and let
JAXBElementProvider do it ?
You can configure JAXBElementProvider to use a stax writer internally and
also set all the marshalling properties, including a namespace prefix
resolver; and just return a JAXB Bean encapsulating the users, perhaps even
returning a collection of users would work...

Let us know how it goes please
cheers, Sergey
On Wed, Apr 28, 2010 at 5:53 PM, McGinn, John <[email protected]>wrote:

> I am seeing some really bizarre behavior for a jaxrs serviterice I've
> written.  I'm a little at a loss at one's going on and this may not be the
> appropriate place as it may be in the bowels of stax or my servlet api.
>
> I am trying to stream a large resultset straight from my database to the
> response output stream.
> 1.  I get an iterator from my database.
> 2.  Then I use stax to start my xml response document.
> 3.  Then I loop over the iterator and use jaxb to marshal those objects to
> xml directly to the output stream.
> 4.  Finally I use stax again to close the document.
>
> My service method looks like this:
>        @GET
>        @Path( "users/roles/{role}/marks/{mark}" )
>        @Produces( "application/xml" )
>        public void getUsersByRoleAndMark( @Context HttpServletResponse
> response, @PathParam( "role" ) String role, @PathParam( "mark" ) String
> mark, @QueryParam ("getRoles") @DefaultValue("false")  String getRoles  ) {
> ...
>
> My marshalling code looks like this:
>        public void streamUsers(LoggedUserIterator users, OutputStream out)
> {
>
>                XMLOutputFactory xof = XMLOutputFactory.newInstance();
>
>                try {
>                        XMLStreamWriter xtw =
> xof.createXMLStreamWriter(out);
>                        xtw.writeStartDocument("utf-8","1.0");
>                        xtw.setPrefix("sso", "
> http://resources.sso.railinc.com/v1";);
>                        xtw.writeStartElement("
> http://resources.sso.railinc.com/v1","users";);
>                        xtw.writeNamespace("sso", "
> http://resources.sso.railinc.com/v1";);
>                        xtw.writeCharacters("");
>                        xtw.flush();
>
>                        Marshaller m = this.jaxbCtx.createMarshaller();
>                        m.setProperty(Marshaller.JAXB_FRAGMENT, true);
>                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
> true);
>
>                        while(users.hasNext()) {
>                                LoggedUser loggedUser = users.next();
>                                User user = convertToLocalUser(loggedUser);
>                                m.marshal(user, out);
>                        }
>
>                        xtw.writeEndElement();
>                        xtw.writeEndDocument();
>
>                        xtw.flush();
>                        out.flush();
>                        xtw.close();
>
>                } catch (IOException e) {
>                        logException(e);
>                        throw500();
>                } catch (JAXBException e) {
>                        logException(e);
>                        throw500();
>                } catch (XMLStreamException e) {
>                        logException(e);
>                        throw500();
>                }
>        }
>
> ******* Now here is the weirdness:
> If my results end up with more than 1 result in the iterator, it works
> fine.  If my results end up with only returning one user, I get an empty
> response with a 200 code.  I've tried the following:
> 1.  Wrapped my OutputStream in one that also logs to a
> ByteArrayOutputStream and yes it is writing the full XML document.
> 2.  Change to use the writer instead of the OutputStream and this actually
> works, but I get the dreaded IllegalStateException writer already obtained
> (even though it seems to work).
> 3.  Used tcpmonitor to confirm it is returning an empty result.
>
> Any ideas?  Any other mailinglist/boards I should pursue?  Anyone else seen
> this?
>
> Thanks
>
> John
>
> This email and any files transmitted with it are confidential and intended
> solely for the use of the individual or entity to whom they are addressed.
> If you have received this email in error please notify the system manager.
> This message contains confidential information and is intended only for the
> individual named. If you are not the named addressee you should not
> disseminate, distribute or copy this e-mail.
>

Reply via email to