gpt4 book ai didi

java - 如何在spring boot属性文件中转义 "@"

转载 作者:行者123 更新时间:2023-11-30 05:35:01 25 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,它连接到 salesforce 以从 salesforce 获取数据并将数据推送到 SQL Server 数据库中。我正在尝试从 application.properties 文件中获取所有属性。我的密码有 @,因此在执行期间读取该属性时,我得到“example”作为我的密码,而不是“example@country”

Eg : 
Pwd = example@country

我检查了一些有用的链接,但没有运气,感谢任何帮助

https://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0204propertiesfileformat01.html

用于获取配置 Prop 的实用程序包。

                package SFToJava.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class configUtility {
@Autowired
private Environment env;

public String getProperty(String pPropertyKey) {
return env.getProperty(pPropertyKey);
}
}

属性文件:

password = example@country

我按如下方式获取服务类中的数据:

            loginParams.add(new BasicNameValuePair("client_id",configUtil.getProperty("consumerKey")));
loginParams.add(new BasicNameValuePair("client_secret", configUtil.getProperty("consumerSecret")));
loginParams.add(new BasicNameValuePair("username", configUtil.getProperty("username")));
loginParams.add(new BasicNameValuePair("password", configUtil.getProperty("password")));

我希望密码在解析到列表时应包含“example@country”,而不是仅获取“example”。

仅供引用。我可以直接使用如下密码连接到 Salesforce

            private static final String username = "******";
private static final String password = "example@coutnry";
private static final String consumerKey = "*************8";
private static final String consumerSecret = "************";

loginParams.add(new BasicNameValuePair("client_id", consumerKey));
loginParams.add(new
BasicNameValuePair("client_secret", consumerSecret));
loginParams.add(new
BasicNameValuePair("grant_type", "password"));
loginParams.add(new
BasicNameValuePair("username", username));
loginParams.add(new
BasicNameValuePair("password", password));

最佳答案

通常情况下,您不必转义 @,也可以在 .properties 文件中使用转义的 \\@:

mail.address=user\\@domain.com

但是我在使用它进行身份验证时遇到了一些问题。在这种情况下,您可能需要使用编码的 %40 来表示 @:

# replace @ in email with %40
mail.address=user%40domain.com

关于java - 如何在spring boot属性文件中转义 "@",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56826479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com