How to encode special characters for a POST with Spring/Roo

穿精又带淫゛_ 提交于 2019-12-23 15:48:42

问题


I'm using Spring/Roo for an app server, and need to be able to post some special characters. Specifically, characters like the Yen symbol, or Euro symbol. When I receive these characters on my server, and display them in console, they appear as "?". How can they be properly encoded and received?


回答1:


There are a couple of possible failure points here.

First, I'd check to see if the console supports the characters in question:

  • if the default encoding used by the JVM does not support the characters, they will be turned into question marks by System.out
  • if the console font does not support the characters, they will not be rendered properly
  • if the console is decoding the bytes using a different encoding to the one System.out is encoding them to, the characters will not display correctly

Instead of trying to print characters as literal, cast to int and print the hex value - then check the value against the Unicode charts.

Lossy or incorrect conversions can also happen between the browser and the server. Ideally, the server should use UTF-8 for encoding and decoding. If the encoding used by the browser when it encodes the data does not support the characters, they will be lossily encoded; the browser usually picks an encoding based on the encoding sent by the server for the GET request (or more rarely from a form attribute). Inspect the Accept-Charset header being sent with your data (you can do this with something like Firebug or Fiddler). I don't know anything about Roo, but there's bound to be some mechanism to configure encodings.




回答2:


Try configuring src/main/resources/META-INF/spring/database.properties to this :

database.url=jdbc:mysql://[YOUR_DB_SERVER]:3306/[YOUR_DB_NAME]?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8


来源:https://stackoverflow.com/questions/2609199/how-to-encode-special-characters-for-a-post-with-spring-roo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!