質問

基本的には、次のようにシンプルな小さなサーブレットを用意しています:

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を取得する方法はありますか。または、ファイルに保存して、そこからマーシャリング解除するだけですか?

役に立ちましたか?

解決

um.unmarshallを呼び出すことにより、 request.getInputStream() String に読み取り、 String を非マーシャリングできます。 (新しいStringReader(str)); そして、その String は、catch-clauseでもアクセス可能になります

他のヒント

それを行うには、リクエスト InputStream を一時バッファー(たとえば、Stringまたはbyte [])に読み込み、JAXBを介して渡す必要があります。ドキュメントが大きい場合、パフォーマンスが低下する可能性がありますが、ドキュメントが大きい場合、JAXBはいずれにせよ遅くなります。

Apache Commons IO を使用する意味がある場合、これは簡単です。

String buffer = IOUtils.toString(request.getReader());
Object obj = jaxbContext.createUnmarshaller().unmarshal(new StringReader(buffer));
// do something else with the buffer here
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top