开发者

Spring request body problem with non-valid xml

开发者 https://www.devze.com 2023-03-08 07:39 出处:网络
In our rest application we use @RequestBody StreamSorce to upload xml file. The problem is that is xml itself is non-valid or contains some invalid characters, PUT request is failing(with http bad req

In our rest application we use @RequestBody StreamSorce to upload xml file. The problem is that is xml itself is non-valid or contains some invalid characters, PUT request is failing(with http bad request response) before our logic, so we can not inform client about exact problem. I know that it is possible to use just plain String for requestBody, but does it make sense to use it? I guess if i will upload 100Mb xml each request will create String request body with same size, and while using Strea开发者_开发问答mSource we are reading input stream while we need it.

What are the cons and pros for using String or StreamSource as requestbody. If i do it with StreamSorce will it scan the whole xml?


You already know the con of the StreamSource: in makes it not possible for you to pre-process the XML, in case it is invalid.

Using String: when XML would be so large, it would be a performance killer! Never use so large objects in Java - in multi-thread environment of application server it can easily lead to OutOfMemoryError, it makes your application vulnerable to DoS attack!

The best solution is to map @RequestBody as InputStream, and process that InputStream with SAX parser. You have low memory consumption (SAX parser doesn't store XML structure in memory) and you can handle exceptions, that can be thrown during the processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号