Pregunta

Tengo un pequeño servlet, básicamente:

void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
    JAXBContext jaxb = myContext.getXMLContext().getSearchparamContext();
    SearchParams params = null;
     try {
        Unmarshaller um = jaxb.createUnmarshaller();
        um.setSchema(myContext.getXMLContext().getSearchParamsSchema()); 
         JAXBElement<SearchParams> je = (JAXBElement<SearchParams>) um.unmarshal(request.getInputStream());
        params = je.getValue();
    } catch (JAXBException e) {
        logger.log(Level.WARNING,"Error parsing xml from user "+ request.getRemoteUser(),e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
...

¿Hay alguna forma de obtener el XML original aquí, si falla el análisis, principalmente para fines de rastreo? ¿O debería simplemente guardar en un archivo y deshacerme de eso?

¿Fue útil?

Solución

Puede leer el request.getInputStream () en una String , y deshacer esa String , llamando a um.unmarshall (nuevo StringReader (str)) ; Entonces esa String también estará accesible en la cláusula catch

Otros consejos

Si desea hacer eso, deberá leer la solicitud InputStream en un búfer temporal (por ejemplo, una Cadena o un byte []), y luego pasarlo a través de JAXB. Si sus documentos son grandes, esto podría afectar su rendimiento, pero si sus documentos son grandes, entonces JAXB será lento de todos modos.

Si tiene el buen sentido de usar Apache Commons IO , esto es fácil:

String buffer = IOUtils.toString(request.getReader());
Object obj = jaxbContext.createUnmarshaller().unmarshal(new StringReader(buffer));
// do something else with the buffer here
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top