Search code examples
javaxmlweb-servicesglassfish-4payara

Webservice and Unmarshalling exception


The current project I'm working on involves calling a number of webservices from a java application. The webservices are hosted on a payara/glassfish server running on a virtualized linux box. The webservices return data from two different legacy systems, one based on a SQLServer database, and the other a FoxPro based database.

Occasionally, the webservice will return data that contains a value (byte) that is not allowed in xml version 1.0, and the application throws an unmarshalling exception, invalid character (0x2) in response. Since I have no control over the data that is fetched from the databases, I need to find a way to filter/replace the offending character so that the application can use the data.

I do have access to the webservice code, so I can make changes to both the service and the clients if need be. I did read somewhere that xml version 1.1 allows for certain control characters, but I'm not sure how to upgrade that version, or even where I would do that.

Suggestions?


Solution

  • Like with this tutorial (https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter6/custom_marshalling.html), you probably can make a custom unmarshaller by implementing readFrom from the interface MessageBodyReader like this:

      Object readFrom(Class<Object>, Type genericType,
                      Annotation annotations[], MediaType mediaType,
                      MultivaluedMap<String, String> httpHeaders,
                      InputStream entityStream)
                             throws IOException, WebApplicationException {
    
          try {
             JAXBContext ctx = JAXBContext.newInstance(type);
             StringWriter writer = new StringWriter();
             IOUtils.copy(inputStream, writer, encoding);
             String theString = writer.toString();
             // replace all special characters
             theString = theString.replaceAll("[\u0000-\u001f]", "");
             return ctx.createUnmarshaller().unmarshal(theString);
          } catch (JAXBException ex) {
            throw new RuntimeException(ex);
          }
       }