枚举常量中的特殊字符(德语元音变音)
给定此枚举类型:
public enum PackageType {
BEHAELTER("BH", "Behälter")
String value;
String id;
String description;
PackageType(String id, String description) {
this.id = id;
this.description = description;
}
}
使用此枚举时,Behälter 中的元音变音“ä”显示错误。即使当我在构造函数中调试并观察属性“描述”时,我也发现它是错误的。
是否有 Java 规范禁止在此类构造中使用任何特殊字符?我现在很无能为力。
我注意到,当将此枚举序列化为 JSON 并将其返回到我的前端时。
谢谢
given this enum type:
public enum PackageType {
BEHAELTER("BH", "Behälter")
String value;
String id;
String description;
PackageType(String id, String description) {
this.id = id;
this.description = description;
}
}
When working with this enum, the umlaut "ä" in Behälter is shown wrong. Even when i debug in the constructor and watch the attribute "description", i see it wrong.
Is there a Java spec which forbids any special chars in constructs like this? I am pretty clueless at this point.
I noticed that when serializing this enum to JSON and returned it to my frontend.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与枚举无关 - 如果您只有这样的控制台应用程序,您可能会看到同样的事情:
可能的问题:
第一个选项是最有可能的。如果是这种情况,很容易测试 - 将字符串更改为
"Beh\u00e4lter"
- 您感兴趣的字符是 U+00E4,这样 Unicode 转义允许您用字符串文字,而不超出实际源代码中的 ASCII。如果这确实有效,那么您可以返回到之前的代码,但检查源文件的实际保存方式以及编译器选项的内容。 (我个人建议使用 UTF-8 作为完整的 Unicode、受良好支持的编码。)
It's nothing to do with enums - you'd probably see the same thing if you just had a console app like this:
Possible problems:
The first option is the most likely, to be honest. If that's the case, it's easy to test - change the string to
"Beh\u00e4lter"
- the character you're interested in is U+00E4, so that Unicode escape allows you to express it in a string literal without going outside ASCII in the actual source code.If that does indeed work, then you could go back to the previous code but check how your source file is actually being saved, and what your compiler options say. (Personally I'd recommend using UTF-8 as a full-Unicode, well-supported encoding.)