JAXB, 원래 XML을 얻지 못한 후 원래 XML을 받으십시오
문제
기본적으로 간단한 작은 서블릿이 있습니다.
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;
}
...
구문 분석이 실패하면 대부분 추적 목적으로 원래 XML을 잡을 수있는 방법이 있습니까? 아니면 파일에 저장하고 그로부터 unmarshal을 저장해야합니까?
해결책
당신은 읽을 수 있습니다 request.getInputStream()
a String
, 그리고 그 점이 ... String
, 전화로 um.unmarshall(new StringReader(str))
; 그 String
캐치 클라스에서도 액세스 할 수 있습니다
다른 팁
그렇게하려면 요청을 읽어야합니다. InputStream
임시 버퍼로 (예 : 문자열 또는 바이트 [])로, JaxB를 통해 전달하십시오. 문서가 커지면 성능이 상처를 줄 수 있지만 문서가 크면 JAXB가 느려질 것입니다.
사용하는 것이 좋습니다 아파치 커먼즈 IO, 이건 쉬운 일입니다.
String buffer = IOUtils.toString(request.getReader());
Object obj = jaxbContext.createUnmarshaller().unmarshal(new StringReader(buffer));
// do something else with the buffer here
제휴하지 않습니다 StackOverflow