Spring+Hibernate Java Web应用印度区域语言返回????问题咨询
Hey there! Let's tackle this frustrating issue where your Telugu text క్రియేటివ్ వర్చ్యువ saves and reads fine directly from the database, but comes back as ???? when fetched via your AJAX request. Here are the most likely fixes to try:
1. Enforce UTF-8 in AJAX Requests & Spring Responses
First, make sure your frontend AJAX request explicitly uses UTF-8. If you're using jQuery, add the contentType parameter:
$.ajax({ url: '/your-endpoint', type: 'POST', contentType: 'application/json; charset=utf-8', // Critical line data: JSON.stringify({ domainId: domainId }), success: function(response) { // Handle response } });
On the Spring backend, force your controller to return UTF-8 encoded JSON. You can do this per endpoint:
@GetMapping("/your-endpoint") @RequestMapping(produces = "application/json; charset=UTF-8") public ResponseEntity<YourResponse> fetchData(@RequestParam Long domainId) { // Your logic here }
Or set it globally with a message converter configuration:
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(); jacksonConverter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(jacksonConverter); } }
2. Verify Database Connection & Hibernate Settings
Even though direct DB reads work, confirm your JDBC URL includes UTF-8 parameters to ensure Hibernate doesn't mangle characters during serialization:
# For MySQL example spring.datasource.url=jdbc:mysql://localhost:3306/your_db?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8
Ensure your entity fields don't have any @Column annotations forcing non-UTF-8 encodings—stick to the default unless you have a specific reason to change it.
3. Fix Servlet Container (Tomcat) Encoding
If you're using Tomcat, update your server.xml Connector to enforce UTF-8 for requests and URIs:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
Add a character encoding filter to your web.xml to ensure all requests are processed as UTF-8:
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4. Validate JSON Serialization (Jackson)
Spring uses Jackson by default for JSON processing—make sure it's configured to use UTF-8. You can explicitly set this in your ObjectMapper:
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setDefaultCharset(StandardCharsets.UTF_8); mapper.setEncoding(JsonEncoding.UTF8); return mapper; }
Start with the encoding filter and Spring message converter fixes—these are the most frequent culprits for this type of character garbling. Let me know if any of these steps get your Telugu text showing up correctly!
内容的提问来源于stack exchange,提问作者Indrabhushan

