查询DB并以Json格式返回结果的通用Java方法

查询DB并以Json格式返回结果的通用Java方法

问题描述:

我要找的东西对我来说似乎很简单,但是我的Google失败了.

What I'm looking for seems pretty straight forward to me, but my googles have failed.

我想要一个允许我运行任何查询并以json格式获取结果的方法.

I want a method that allows me to run any query and get the results in json format.

诀窍是我希望结果需要Java对象作为流程的一部分(DTO,VO等).

The trick is I don't want the results to need java objects as part of the process (DTOs, VOs, etc).

是否有任何快速/简便/干净的方法?

Any quick/easy/clean ways of doing this?

杰克逊(Jackson)有一些不错的方法.在此答案中有一些示例可以为您带来奇迹

Jackson has some pretty nice ways of doing it. There's some examples in this answer that should work wonders for you.

或者,如果您无法使用Jackson,您可以查看

Alternatively, if Jackson isn't available to you, you could check out this