Pergunta

Eu tenho um pequeno servlet simples, basicamente:

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;
    }
...

Existe alguma maneira de conseguir o XML original aqui, se a análise falhar - principalmente para fins de rastreamento. Ou devo apenas economizar em um arquivo e soltar a partir disso?

Foi útil?

Solução

Você pode ler o request.getInputStream() dentro de String, e UNUSSHAL que String, ligando um.unmarshall(new StringReader(str)); Então isso String também estará acessível na cláusula de captura

Outras dicas

Se você quiser fazer isso, você precisará ler o pedido InputStream em um buffer temporário (por exemplo, uma string ou um byte []) e depois passe isso através do JAXB. Se seus documentos forem grandes, isso poderá prejudicar seu desempenho, mas se seus documentos forem grandes, o JAXB será lento de qualquer maneira.

Se você tem o bom senso de usar Apache Commons io, então isso é fácil:

String buffer = IOUtils.toString(request.getReader());
Object obj = jaxbContext.createUnmarshaller().unmarshal(new StringReader(buffer));
// do something else with the buffer here
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top