Comments (7)
Sounds fine to me (except that obviously UTF-8 is not going to work for everyone, so we should only assert that if it isn't explicitly set). Any idea where to insert those checks? Pull requests always welcome.
from spring-boot.
Updated research: 'sun.jnu.encoding' is (obviously) platform specific, but it seems to be dependent (at least on *nix machines) on the LANG
environment variable, so maybe it makes sense to check that variable as well or instead?
from spring-boot.
Both of these system properties get their default values, if not specifically set, from the environmental variables LANG
and LC_*
on Linux and Solaris. I suspect that the *BSD ports have followed a similar convention, but haven't checked it.
http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/tip/src/solaris/native/java/lang/java_props_md.c
365 java_props_t *
366 GetJavaProperties(JNIEnv *env)
http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/tip/src/share/native/java/lang/System.c
236 PUTPROP(props, "file.encoding", sprops->encoding);
237 PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
from spring-boot.
On application level, I've currently implemented this like so:
@Bean
public CommandLineRunner requireInternationalEncodingOrCrash(final ConfigurableApplicationContext context) {
return new CommandLineRunner() {
@Override public void run(final String... args) throws Exception {
final String encoding = System.getProperty("file.encoding");
if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())) {
log.error("Your system property \"file.encoding\" is currently \"{}\". It should be \"UTF-8\". ", encoding);
log.error("Your environmental variable LANG is currently \"{}\". It should end \".UTF-8\". ", System.getenv("LANG"));
log.error("Your environmental variable LC_ALL is currently \"{}\". It should end \".UTF-8\". ", System.getenv("LC_ALL"));
log.error("We can't run this application properly if the Java Virtual Machine it runs in hasn't been configured to use the UTF-8 encoding. Closing down.");
context.close();
}
}
};
}
With a little more complexity for various LC_*
combinations.
I assume a properly explicit and contained solution would be to create a "sanity check" stage where we check whether the application can be run in org.springframework.boot.SpringApplication::run
?
from spring-boot.
That's a good start, but yes, I think a better solution might be to use an ApplicationContextInitializer (one that runs after the ConfigFile* one so that it can be parameterized externally).
from spring-boot.
private final static ApplicationContextInitializer<ConfigurableApplicationContext> REQUIRE_UTF8 =
new ApplicationContextInitializer<ConfigurableApplicationContext>() {
private final static String REFUSE_TO_START =
"We can't run this application properly, since the Java Virtual Machine it runs in hasn't " +
" been configured to use the UTF-8 default character encoding. Closing down.";
@Override public void initialize(final ConfigurableApplicationContext ctx) {
final String encoding = System.getProperty("file.encoding");
if (encoding != null && !"UTF-8".equals(encoding.toUpperCase())) {
log.error("Your system property \"file.encoding\" is currently \"{}\". It should be \"UTF-8\". ", encoding);
log.error("Your environmental variable LANG is currently \"{}\". You must use a UTF-8 locale setting.",
System.getenv("LANG"));
log.error("Your environmental variable LC_ALL is currently \"{}\". You must use a UTF-8 locale setting.",
System.getenv("LC_ALL"));
log.error(REFUSE_TO_START);
throw new IllegalStateException(REFUSE_TO_START);
}
}
};
public static void main(final String ... args) {
new SpringApplicationBuilder(ExampleController.class).initializers(REQUIRE_UTF8).run(args);
}
from spring-boot.
Thanks :)
from spring-boot.
Related Issues (20)
- Using Gradle's new file permission API is implemented in a way that prevents removal of the old API
- Support loading config yaml files embedded in env vars via spring.config.import HOT 1
- mapstruct-processor jar is missing when package from springboot2.4 HOT 3
- Reduce warnings reported by Eclipse
- Migrate class references to full javadoc links
- Use javadoc macro for links in adoc files
- Reduce warnings reported by Eclipse
- Rename one instance of DataLdapTestIntegrationTests in spring-boot-test-autoconfigure
- Rename one instance of DataLdapTestIntegrationTests in spring-boot-test-autoconfigure
- Spring Jersey does not return 404 from Controller if entity is null HOT 3
- Some @ControllerEndpoint and @RestControllerEndpoint infrastructure remains undeprecated
- @ConditionalOnProperty for branch of values not match HOT 3
- Update link to documentation for log4j-spring-boot
- Update link to documentation for log4j-spring-boot
- Move "Customize the whitelabel Error Page" how-to
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- Upgrade to Testcontainers 1.20.0 HOT 1
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- Improve Configuration Experience for Multiple CacheManagers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-boot.