如何在属性文件Spring Boot中转义“ @”

时间:2019-06-30 16:41:08

标签: java spring-boot properties-file

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

Eg : 
Pwd = example@country

我检查了一些有用的链接,但没有运气,不胜感激

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

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

                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));

1 个答案:

答案 0 :(得分:0)

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

mail.address=user\\@domain.com

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

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