2016-05-25 17 views
10

में 0.में एप्लिकेशन.प्रोपर्टीज की यूटीएफ -8 एन्कोडिंग विशेषताएँ कुछ कस्टम विशेषताओं को जोड़ती हैं।स्प्रिंग-बूट

custom.mail.property.subject-message=This is a ä ö ü ß problem 

इस कक्षा में मेरे पास कस्टम विशेषताओं का प्रतिनिधित्व है।

@Component 
@ConfigurationProperties(prefix="custom.mail.property") 
public class MailProperties { 
    private String subjectMessage; 
    public String getSubjectMessage() { 
     return subjectMessage; 
    } 
    public void setSubjectMessage(String subjectMessage) { 
     this.subjectMessage = subjectMessage; 
    } 

और यहाँ मैं का उपयोग अपने MailProperties:

@Service 
public class SimpleUnknownResponseMessage extends MailProperties implements UnknownResponseMessage{ 

    private JavaMailSender javaMailSender; 

    @Autowired 
    public SimpleUnknownResponseMessage(JavaMailSender javaMailSender) { 
     this.javaMailSender = javaMailSender; 
    } 

    @Override 
    public void placeUnknownResponse(BookResponse bookResponse) { 
     MimeMessage message = javaMailSender.createMimeMessage(); 
     try { 
      MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); 
      helper.setSubject(this.getSubjectMessage());    
      javaMailSender.send(message); 

     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
    } 

जबकि डिबगिंग मैं अपने this.getSubjectMessage() चर यह मान के अंदर है कि देख सकते हैं: This is a ä ö ü à problem। तो मेरे मेल भेजने से पहले मेरे पास पहले से ही एक यूटीएफ -8 एन्कोडिंग समस्या है।

मैंने पहले ही application.properties फ़ाइल और उसके यूटीएफ -8 के एन्कोडिंग की जांच की है।

मेरा आईडीई (एसटीएस/ग्रहण) और परियोजना गुण यूटीएफ -8 पर भी सेट हैं।

application.properties फ़ाइल में मेरे कस्टम विशेषताओं के टेक्स्ट के लिए मैं यूटीएफ -8 एन्कोडिंग कैसे सेट कर सकता हूं?

+2

मुझे याद है कि जावा कहीं उम्मीद करता है कि '* .properties' फ़ाइलों को आईएसओ -885 9 -1 में एन्कोड किया गया है और यही कारण है कि वसंत 'application.properties' का व्यवहार करता है जैसे कि यह आईएसओ -885 9 -1 में है। यह प्रश्न देखें: [स्प्रिंग बूट डिफ़ॉल्ट गुण एन्कोडिंग परिवर्तन?] (Http://stackoverflow.com/questions/27882191/spring-boot-default-properties-encoding-change)। संभावित समाधान: गुण फ़ाइलों के बजाय YAML का उपयोग करें। – Jesper

+0

@ जेस्पर हां, मैंने भी इस जवाब की जांच की। अभी भी उम्मीद है कि संपत्ति फ़ाइलों के लिए शायद एक और समाधान है। – Patrick

उत्तर

12

जैसा कि पहले से ही टिप्पणियों में उल्लेख किया गया है। प्रोपर्टीज फ़ाइलों को आईएसओ 885 9 -1 में एन्कोड किया जाने की उम्मीद है। कोई अन्य वर्ण निर्दिष्ट करने के लिए यूनिकोड एस्केप का उपयोग कर सकता है। रूपांतरण करने के लिए tool भी उपलब्ध है। उदाहरण के लिए यह स्वचालित निर्माण में उपयोग किया जा सकता है ताकि आप अभी भी स्रोत में अपने पसंदीदा एन्कोडिंग का उपयोग कर सकें।

+0

एक वर्कअराउंड के लिए यूनिकोड से बचने के लिए अपवोट – Patrick

6

मुझे एक ही समस्या का सामना करना पड़ा है। स्प्रिंग बूट में 2 PropertySourceLoader जो आवेदन में गुण लोड करने के लिए उपयोग किया जाता है देखते हैं:

  • PropertiesPropertySourceLoader - UTF-8 केवल जब एक्सएमएल
  • YamlPropertySourceLoader से लोड - UTF-8 का समर्थन करता है, लेकिन आप यह

वे फ़ाइल में सूचीबद्ध कर रहे हैं का उपयोग करने के विन्यास प्रारूप बदलना होगा https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/META-INF/spring.factories

इसलिए हमने PropertySourceLoader का अपना कार्यान्वयन लिखने का निर्णय लिया जो यूटीएफ -8 फ़ाइल से गुणों को सही ढंग से लोड करने में सक्षम होगा। विचार जवाब @BalusC से है - How to use UTF-8 in resource properties with ResourceBundle

हमारे PropertySourceLoader कार्यान्वयन:

public class UnicodePropertiesPropertySourceLoader implements PropertySourceLoader { 

@Override 
public String[] getFileExtensions() { 
    return new String[]{"properties"}; 
} 

@Override 
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException { 
    if (profile == null) { 
     Properties properties = new Properties(); 
     PropertyResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(resource.getInputStream(), "UTF-8")); 
     Enumeration<String> keys = bundle.getKeys(); 
     while (keys.hasMoreElements()) { 
      String key = keys.nextElement(); 
      properties.setProperty(key, bundle.getString(key)); 
     } 
     if (!properties.isEmpty()) { 
      return new PropertiesPropertySource(name, properties); 
     } 
    } 
    return null; 
} 

} 

तो हम फ़ाइल बनाई संसाधनों/META-INF/वसंत।कारखानों सामग्री के साथ:

  • YamlPropertySourceLoader
  • नोट

    • UnicodePropertiesPropertySourceLoader
    • PropertiesPropertySourceLoader:

      # Custom PropertySource Loaders 
      org.springframework.boot.env.PropertySourceLoader=\ 
      your.own.package.UnicodePropertiesPropertySourceLoader 
      

      अब हम इस क्रम में हमारे आवेदन में 3 PropertySourceLoader है!

      1. मुझे यकीन है कि यह PropertyResourceBundle
      2. का समुचित उपयोग मुझे यकीन है कि वसंत बूट में PropertySourceLoaders के आदेश में एक ही यदि आप अन्य परियोजनाओं में यह पुन: उपयोग करने के लिए एक समर्पित पुस्तकालय बनाने के लिए किया जाएगा नहीं कर रहा हूँ है नहीं कर रहा हूँ।

      हमारी परियोजना में यह समाधान ठीक काम करता है।

      अद्यतन!

      यह PropertyResourceBundle बिना UnicodePropertiesPropertySourceLoader का भार विधि लागू करने के लिए बेहतर है:

      @Override 
      public PropertySource<?> load(String name, Resource resource, String profile) throws IOException { 
          if (profile == null) { 
           Properties properties = new Properties(); 
           properties.load(new InputStreamReader(resource.getInputStream(), "UTF-8")); 
           if (!properties.isEmpty()) { 
            return new PropertiesPropertySource(name, properties); 
           } 
          } 
          return null; 
      } 
      
    1

    कृपया अपना configuaration फ़ाइल में एन्कोडिंग पैरामीटर के साथ PropertySource टिप्पणी जोड़ने के लिए प्रयास करें:

    @PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8") 
    

    आशा है कि यह मदद करता है।

    संबंधित मुद्दे