This discussion is archived
1 Reply Latest reply: Oct 7, 2011 3:51 PM by 801313 RSS

Trying to create a web service in NetBeans

801313 Explorer
Currently Being Moderated
Hi Folks let's cut right to the chase

I've followed the demos / tutorials and built a few simple web services running on glassfish / mysql / netBeans IDE. All well and good. What I really want now is to be able to return an XML document or a string with xml data from my service.

I tried building an XML document using xerces / document builder (hopefully this is familiar to you) Unfortunately, the results I get back are not XML encoded, and also as I add elements & attributes to my document, it often creates a transformException (which I don't get when doing it from the console). I also get an exception if I try to add two elements as children to the same parent, as indicated in the code snippet below - I don't know what I'm doing wrong there.
          Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
          doc.createElement("Word");
          Element top = doc.createElement("myapp");
        Element word = doc.createElement("word");//new Element("word");
        //Attr attr = doc.createAttribute("testval");
        word.setAttribute("test", "חֹשֶׁך");
        //word.setNodeValue("חֹשֶׁך");
        word.setTextContent("חֹשֶׁך");
        doc.appendChild(top);
        top.appendChild(word);
        Element word2 = doc.createElement("word");
        top.appendChild(word2);
So I redid my web service just using a PrintWriter / StringWriter, and at the end replacing '<' and '>' with &lt; &gt; That's all well and good, and I get back the proper response, with the xml tags showing. But I still get an error message: Service invocation threw an exception with message :

" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.; Refer to the server log for more details"

(more details at the very bottom). As you may have noticed in the code snippet I posted above, I'm using strings of hebrew characters.

I don't understand how I can get a response back and STILL get a Service invocation exception message.

I also don't know how to fix whatever's causing the exception. Anyone dealt with this kind of thing before?

Thanks! (Code below)
    private static void addWord(PrintWriter pw, Words w, List<Definition> definitions) {
        //Element word = doc.createElement("word");//new Element("word");
        pw.append("<Word>");
        pw.append("<HW>" + w.getWord() + "</HW>");
        
//        word.setTextContent(w.getWord());        
//        doc.appendChild(word);
        for (Definition def : definitions) {
  //          Element definition = doc.createElement("Definition");
            pw.append("<Definition>" + def.getDefinition() + "</Definition>");
  //          definition.setTextContent(def.getDefinition());
            String pos = def.getPos();
            if (pos == null) pos = "";
            String stem = def.getStem();
            if (stem == null) stem = "";
            pw.append("<POS>" + pos + "</POS>");
            pw.append("<Stem>" + stem + "</Stem>");
//            Element posElem = doc.createElement("POS");
//            posElem.setTextContent(pos);
//            Element stemElem = doc.createElement("Stem");
//            stemElem.setTextContent(stem);
//            definition.setAttribute("POS", def.getPos());
//            definition.setAttribute("stem", def.getStem());
//            word.appendChild(definition);
//            word.appendChild(posElem);
//            word.appendChild(stemElem);
        }
        pw.append("</Word>");
    }
SOAP Response

Service invocation threw an exception with message : com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.; Refer to the server log for more details


Exceptions details : javax.xml.transform.TransformerException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.

javax.servlet.ServletException: javax.xml.transform.TransformerException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence. at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:330) at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:106) at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:114) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.doFilter(ServletAdapter.java:1002) at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.invokeFilterChain(ServletAdapter.java:942) at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:404) at com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:354) at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168) at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:234) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:662) Caused by: javax.xml.transform.TransformerException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence. at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:719) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313) at org.glassfish.webservices.monitoring.WebServiceTesterServlet.dumpMessage(WebServiceTesterServlet.java:362) at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:320) ... 24 more Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence. at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684) at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:369) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1416) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2792) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:609) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points