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
最满意答案
考虑你的编辑
@PropertySource(value="file:#{systemProperties['config.path']}"
你必须首先意识到
@Configuration
类(和它的注释)在它内部声明的@Bean
之前被解析和处理。 换句话说,Spring处理@PropertySource
批注时尚未注册PropertySourcesPlaceholderConfig
(它解析#{..}
占位符)。 它不可用。相反,Spring可以执行
PropertySourcesPlaceholderConfig
提供的行为的子集,只有${..}
占位符。 ( 请参阅javadoc 。)并且它只能使用已在ApplicationContext
的Environment
注册的属性源。 在您的示例中,(应该)只有两个来源,即系统属性和系统环境。符号
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, yourPropertySourcesPlaceholderConfig
(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 yourApplicationContext
'sEnvironment
. 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 namedsystemProperties
. 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 aconfig.path
property, you can simply use@PropertySources(value = { @PropertySource(value = "file:${config.path}") })
相关问答
更多-
export CONF_DIR=/usr/certain_path/config.properties 如果不使用export ,则仅在当前环境中设置。 如果将其导出,则它将成为环境变量,并由子进程继承。 export CONF_DIR=/usr/certain_path/config.properties If you don't use the export, it's only set in the current environment. If you export it, then it beco ...
-
你是对的,这个问题如何适用于很多人(例如开发Windows环境与产品Unix环境等),这很奇怪。 一个自然的答案是,你只需要将正确的尾部“斜线”放在实际dir属性的末尾,格式与操作系统特定的文件路径类型相同。 除此以外... 这里有一个解决方案,假设你在操作系统环境本地文件系统格式中给出了${dir} ,并且有一个name文件路径,你可以这样做: @PropertySource(name = "theFileInDir.properties",value = { "file:${dir}" }, facto ...
-
我通过使用PropertySourcesPlaceholderConfigurer而不是PropertyPlaceholderConfigurer解决了这个问题 I solved it by using PropertySourcesPlaceholderConfigurer instead of PropertyPlaceholderConfigurer
-
我已经解决了这个问题如下。 我已经意识到从资源包中获取属性然后在bean上设置属性之间存在区别 - Spring将在使用相关PropertyEditor(ResourceEditor)设置属性时进行转换。 所以我们必须手动执行此步骤: @Configuration @PropertySource("classpath:my.properties") public class TestConfig { @Autowired Environment environment; @Bean ...
-
考虑你的编辑 @PropertySource(value="file:#{systemProperties['config.path']}" 你必须首先意识到@Configuration类(和它的注释)在它内部声明的@Bean之前被解析和处理。 换句话说,Spring处理@PropertySource批注时尚未注册PropertySourcesPlaceholderConfig (它解析#{..}占位符)。 它不可用。 相反,Spring可以执行PropertySourcesPlaceholderCon ...
-
我能解决我的问题。 我尝试了几种不同的方法,包括Spring AOP和LTW,这些方法在尝试实例化方面过早时不起作用 - 以便VaultPropertySource在Eureka的DiscoveryClient拨打电话之前可用。 我的解决方案包括: 为了比Eureka bean更早实例化Vault bean并且配置开始实例化: @Ordered或实施VaultPropertySourceLocator中的VaultPropertySourceLocator 。 我的VaultPropertySourceLo ...
-
使用PropertyPlaceholderConfigurer和PropertySource(Using both PropertyPlaceholderConfigurer and PropertySource)[2023-06-25]
随着 ctx.getEnvironment().getPropertySources().addFirst(ps); 你走对了路。 我认为您的MyPropertyPlaceholderConfigurer (来自该Enterprise Framework)不仅与PropertySourcesPlaceholderConfigurer兼容。 您应该查看他们的代码并覆盖它以扩展PropertySourcesPlaceholderConfigurer 。 从另一端PlaceholderConfigurerSu ... -
从属性文件中读取值远远优于在类文件中对它们进行硬编码。 如果你硬编码,那么你需要重新编译,如果你想更改其中任何一个。 回答你的批评: 1。 系统属性是可迭代的,而PropertySource则不是 大多数PropertySources扩展EnumerablePropertySource。 虽然我不确定你想要迭代你的属性的用例 2。 PropertySource不允许回退属性 - 并且创建自定义PropertySource至少等于对系统属性执行相同操作的代码。 您可以使用标准的spring属性getter,而 ...
-
下面似乎工作正常,但我不确定实例是否始终是ConfigurableEnvironment类型 @Component public class MyListener implements ApplicationListener
{ @Autowired private Environment env; private static final Logger log = LoggerFactory.getLogger(MyListener.class) ... -
Spring无法使用@PropertySource找到classpath资源(Spring not able to find classpath resource with @PropertySource)[2023-04-11]
对于任何在Spring 4+中使用.properties文件时遇到问题的人,请查看下面的线程以使您的设置与OP的设置相匹配。 除了配置中的空白外,安装程序都很好。 无法使用@PropertySource将.properties文件注入到Spring MVC 4.3中 For anybody facing problem with usign .properties files in Spring 4+, look at the thread below to match your setup with th ...