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();
          Element top = doc.createElement("myapp");
        Element word = doc.createElement("word");//new Element("word");
        //Attr attr = doc.createAttribute("testval");
        word.setAttribute("test", "חֹשֶׁך");
        Element word2 = doc.createElement("word");
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 :

" 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("<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);
SOAP Response

Service invocation threw an exception with message : Invalid byte 2 of 2-byte UTF-8 sequence.; Refer to the server log for more details

Exceptions details : javax.xml.transform.TransformerException: Invalid byte 2 of 2-byte UTF-8 sequence.

javax.servlet.ServletException: javax.xml.transform.TransformerException: Invalid byte 2 of 2-byte UTF-8 sequence. at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost( at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke( at org.glassfish.webservices.EjbWebServiceServlet.service( at javax.servlet.http.HttpServlet.service( at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.doFilter( at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.invokeFilterChain( at com.sun.grizzly.http.servlet.ServletAdapter.doService( at com.sun.grizzly.http.servlet.ServletAdapter.service( at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service( at com.sun.enterprise.v3.server.HK2Dispatcher.dispath( at at com.sun.grizzly.http.ProcessorTask.invokeAdapter( at com.sun.grizzly.http.ProcessorTask.doProcess( at com.sun.grizzly.http.ProcessorTask.process( at com.sun.grizzly.http.DefaultProtocolFilter.execute( at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter( at com.sun.grizzly.DefaultProtocolChain.execute( at com.sun.grizzly.DefaultProtocolChain.execute( at com.sun.grizzly.http.HttpProtocolChain.execute( at com.sun.grizzly.ProtocolChainContextTask.doCall( at at at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork( at com.sun.grizzly.util.AbstractThreadPool$ at Caused by: javax.xml.transform.TransformerException: Invalid byte 2 of 2-byte UTF-8 sequence. at at at org.glassfish.webservices.monitoring.WebServiceTesterServlet.dumpMessage( at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost( ... 24 more Caused by: Invalid byte 2 of 2-byte UTF-8 sequence. at at at at at$ at at at at at at at at at


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