首页 \ 问答 \ Spring&MongoCursor&Jackson JsonNode(数据库转换)(Spring & MongoCursor & Jackson JsonNode (Database conversion))

Spring&MongoCursor&Jackson JsonNode(数据库转换)(Spring & MongoCursor & Jackson JsonNode (Database conversion))

我有一个解析从MongoCursor获得的字符串来处理JsonNode的问题。 我试图让MongoCursor返回的Json与我的Spring SQL POJO一起工作,所以我可以将它插入到我的SQL数据库中。 基本上这是一个数据库转换,SQL结束仅用于历史存储。 我没有使用spring的mongo,因为这些字段与POJO有些不同(MongoDB和SQL的模式略有不同)

目前,它的工作原理是使用模式匹配器/字符串拆分,然后替换HashMap,以便我可以获得每个字段的键和值对,然后将其插入到我的弹簧POJO中。 我知道我也可以使用杰克逊的POJO工作,但被告知使用jsonNode作为更好的解决方案。 必须有一些我错过了。

在Jackson文档中,“json”字符串的格式是:

{\“color \”:\“Black \”,\“type \”:\“BMW \”}

然而,MongoCursor回报我的情况并非如此。 用光标,我得到如下所示:

文档{{_ id = G8HQW9123,用户=测试}}

其中我使用了字符串模式匹配器并将replaceAll简化为:

{_id:G8G8HQW9123,用户:测试}

然而,杰克逊的斜杠和双引号让我失望,无法解析。 我错过了什么吗? 或者我必须在我的代码中实际添加斜线和引号才能使其工作? 目前正在获取请求双引号的分析错误。


I have a problem parsing a String I got from MongoCursor to work with JsonNode. I'm trying to get the MongoCursor's returned Json to work with my Spring SQL POJO, so I can insert it to my SQL database. Basically this is a database conversion, and SQL end is just for history storage. I didn't use spring's mongo, because the fields are somewhat different than the POJO's.(MongoDB and SQL have slightly different schema)

Currently, it works by using pattern matcher/ string split and replace then HashMap them so I can get a key and value pair of each field and then insert that into my spring POJO. I know I can also use jackson's POJO to work, but was told to use jsonNode as a better solution. There must be something i'm missing.

In the Jackson docs, the format of a "json" string is:

{ \"color\" : \"Black\", \"type\" : \"BMW\" }

However, that is not going to be the case of what MongoCursor returns me with. With the cursor, I get something like:

Document{{_id=G8HQW9123, User=test}}

which I used string pattern matcher and replaceAll to reduce to:

{_id:G8G8HQW9123, User:test}

However, jackson's slashes and double quotes are throwing me off and unable to parse that. Am I missing something? or do I have to actually add in those slashes & quotes in my code to make things work? Currently getting parse error which requests double quotes.


原文:https://stackoverflow.com/questions/43564646
更新时间:2022-10-10 09:10

最满意答案

考虑你的编辑

@PropertySource(value="file:#{systemProperties['config.path']}" 

你必须首先意识到@Configuration类(和它的注释)在它内部声明的@Bean之前被解析和处理。 换句话说,Spring处理@PropertySource批注时尚未注册PropertySourcesPlaceholderConfig (它解析#{..}占位符)。 它不可用。

相反,Spring可以执行PropertySourcesPlaceholderConfig提供的行为的子集,只有${..}占位符。 ( 请参阅javadoc 。)并且它只能使用已在ApplicationContextEnvironment注册的属性源。 在您的示例中,(应该)只有两个来源,即系统属性和系统环境。

符号

systemProperties['config.path']

states:在名为systemProperties的属性源中为我提供属性config.path的值。 我不确定Spring检查上面命名的属性源的顺序,但是如果你知道只有你的系统属性(而不是环境)有config.path属性,你可以简单地使用

@PropertySources(value = { @PropertySource(value = "file:${config.path}") })

Considering your edit

@PropertySource(value="file:#{systemProperties['config.path']}" 

you have to first realize that the @Configuration class (and its annotations) is parsed and processed before the @Bean declared inside it. In other words, your PropertySourcesPlaceholderConfig (which does resolution of #{..} placeholders) has not been registered by the time Spring has processed the @PropertySource annotation. It's not available.

Instead, Spring can perform a subset of the behavior provided by a PropertySourcesPlaceholderConfig, only the ${..} placeholders. (See the javadoc.) And it can only use the property sources already registered with your ApplicationContext's Environment. In your examples, there are (should be) only two sources, the system properties and system environment.

The notation

systemProperties['config.path']

states: give me the value of the property config.path in the property source named systemProperties. I'm not sure in which order Spring checks the property sources named above, but if you know that only your system properties (and not the environment) has a config.path property, you can simply use

@PropertySources(value = { @PropertySource(value = "file:${config.path}") })

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。