Skip to main content

"xmlns has been already bound" error from XMLStreamWriterImpl w/ valid XML

8 replies [Last post]
alecbritton
Offline
Joined: 2007-10-15

I have a web service deployed to Glassfish and whenever it returns a response that marshals into XML with one or more nested elements the container fails to write the response and issues the following message:

"javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to is an error"

Note that "" is an actual namespace that I have removed for security reasons.

The source of this error is com.sun.xml.stream.writers.XMLStreamWriterImpl.writeDefaultNamespace(XMLStreamWriterImpl.java:791)

I cannot share with you the actual XML but when I unmarshal my response object to XML it is of a form similar to below:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

Text

If I modify the response to only contain the root element the response XML is wriiten without error, so I believe that this class is for some reason taking issue with the use of the default namespace. Is there some reason why the use of namespaces above is considered invalid by XMLStreamWriterImpl? I understand that it is prefereable to use a NS prefix rather than relying on the default NS behavior, but this is how JAXB marshals my XJC-generated object and I am not sure how to change this behavior.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rpollack
Offline
Joined: 2007-06-12

Interestingly (because this class does not appear in the stack trace), the problem is in com.sun.xml.ws.streaming.DOMStreamReader, and it has been fixed; but the fix has not yet been incorporated into GlassFish.

You can see the fix at http://fisheye5.cenqua.com/browse/jax-ws-sources/jaxws-ri/rt/src/com/sun...
It's the line numbered 286 that says
if(fixNull(a.getNamespaceURI()).length()>0)

You can get the new jar by doing the following:
1. Install the latest JAX-WS RI from https://jax-ws.dev.java.net/2.1.3/
2. Extract the classes DOMStreamReader and DOMStreamReader$Scope from the jar jaxws-rt.jar
3. Insert these classes into your GlassFish webservices-rt.jar, making sure that you preserve the path (I did this with WinZip).

You have to modify webservices-rt.jar itself because a lot of configuration files mention it by name. I tried putting the JAX-WS jar into an "endorsed" directory, but this leads to a lot of classloader problems.

The problem has to do with attributes that don't have prefixes. If such an attribute is present (and almost all attributes are like this) in a node with a default namespace defiinition, the problem will occur. Here's a program that reproduces the problem. If you run this with a classpath that uses the unmodified webservices-rt.jar, you'll see the exception. You can verify that the problem goes away if you get this class from the JAX-WS jar or from a modified webservices-rt.jar.

----------------------------------------------------------------------------------

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import com.sun.xml.ws.streaming.DOMStreamReader;
import com.sun.xml.ws.util.xml.XMLStreamReaderToXMLStreamWriter;

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class CodecTest {

static final String input1 =
"";
static final String input2 =
"";

static XMLStreamReaderToXMLStreamWriter bridger =
new XMLStreamReaderToXMLStreamWriter();

static public void main (String[] args) throws Exception {

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer =
outputFactory.createXMLStreamWriter (System.out);
// System.out.println (writer); // throws UnsupportedOperationException

showBridge (1, input1, writer);
showBridge (2, input2, writer);
}

static void showBridge
(int num, String input, XMLStreamWriter writer)
throws Exception
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware (true);

DocumentBuilder parser = dbFactory.newDocumentBuilder();
ByteArrayInputStream bais =
new ByteArrayInputStream (input.getBytes());

Document doc = parser.parse (bais);
Element root = doc.getDocumentElement();
XMLStreamReader reader = new DOMStreamReader (root);

System.out.println();
System.out.println ("Input" + num + ":");
System.out.println (input);
System.out.println ("Output" + num + ": ");

try {
bridger.bridge (reader, writer);
} catch (Exception e) {
e.printStackTrace (System.out);
}
writer.flush();
System.out.println();
}
}

bhaktimehta
Offline
Joined: 2004-03-18

If you take the latest GF v2.1 builds this should be in there.
https://glassfish.dev.java.net/downloads/v2.1-b20.html

Regards,
Bhakti

rpollack
Offline
Joined: 2007-06-12

I've unzipped and installed this release, but I can't get NetBeans (version 6.1, Build 20071228165830) to recognize it. Do you know of any instructions about how to get a new version of GlassFish into NetBeans?

Thanks.

bhaktimehta
Offline
Joined: 2004-03-18

Go to Tools Servers->Add Server. Give the location of your GF installation

rpollack
Offline
Joined: 2007-06-12

Note to others who might make the same mistake I did:

After you unpack the new version of GlassFish according to the instructions, you MUST NOT move it. So choose your location BEFORE you do the final step (lib\ant\bin\ant -f setup.xml).

jitu
Offline
Joined: 2003-06-14

Can you give the full stack trace. Is this with GlassfishV2 ?

alecbritton
Offline
Joined: 2007-10-15

My glassfish version as reported on the admin console is 9.1_01 (build b04-fcs).

Another detail about this error: I discovered that when I remove the handler chain annotation from my service class the web service functions as expected. When I add back in a handler that simply logs the payload to System.out I get the error again, so I think this may have something specifically to do with the execution path taken by the container when processing handlers. I do not believe the handler itself as the error occurs after a message logged by the close() method of the handler, indicating that control has returned to the container.

The full stack trace is:

[#|2007-10-17T10:35:31.906-0700|SEVERE|sun-appserver9.1|javax.enterprise.system.container.web|_ThreadID=15;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=1508247e-ebce-43d5-8013-b62a3f8940c0;|StandardWrapperValve[EmktService]: PWC1406: Servlet.service() for servlet EmktService threw exception
javax.servlet.ServletException
at com.sun.enterprise.webservice.JAXWSServlet.doPost(JAXWSServlet.java:164)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:270)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:339)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:261)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:212)
at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: javax.xml.ws.WebServiceException: javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to is an error
at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:111)
at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:265)
at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:320)
at com.sun.xml.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:93)
at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:454)
at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:135)
at com.sun.enterprise.webservice.JAXWSServlet.doPost(JAXWSServlet.java:159)
... 29 more
Caused by: javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to is an error
at com.sun.xml.stream.writers.XMLStreamWriterImpl.writeDefaultNamespace(XMLStreamWriterImpl.java:791)
at com.sun.xml.stream.writers.XMLStreamWriterImpl.writeNamespace(XMLStreamWriterImpl.java:1008)
at com.sun.xml.ws.util.xml.XMLStreamReaderToXMLStreamWriter.handleStartElement(XMLStreamReaderToXMLStreamWriter.java:165)
at com.sun.xml.ws.util.xml.XMLStreamReaderToXMLStreamWriter.bridge(XMLStreamReaderToXMLStreamWriter.java:97)
at com.sun.xml.ws.message.stream.StreamMessage.writePayloadTo(StreamMessage.java:272)
at com.sun.xml.ws.message.stream.PayloadStreamReaderMessage.writePayloadTo(PayloadStreamReaderMessage.java:109)
at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:142)
at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:108)
... 36 more

Message was edited by: alecbritton

sparefroh
Offline
Joined: 2007-06-18

Hi,

I am facing the same problem. Did you find any solution for that yet?

Thanks!