I have problem with using Spring for loading UTF-8 file.
This is what is working for me :
I have properties file, saved as UTF-8 with this content
global.variable.try=This is product variable
cache.location.filename.regions=regions
hacky.carky=éíáščýéíšž hehe haha hoho +íšářá
In my controller, I access it in two ways
@Controller
@RequestMapping(value = "/aserver")
public class AServerController {
@Value("${hacky.carky}")
private String hackyCarky;
@RequestMapping(value = "/hackycarky", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object hackycarky(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
return hackyCarky;
}
@RequestMapping(value = "/regions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object regions(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
String filePath = "c:\\prace\\eclipse workspace\\czechtraditions\\server\\src\\main\\resources\\server-general.properties";
return new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
}
}
If I access /aserver/hackycarky, it gives desired output :
éíáščýéíšž hehe haha hoho +íšářá
However if I access /aserver/regions, the output is as following :
global.variable.try=This is product variable
cache.location.filename.regions=regions
hacky.carky=���??���?? hehe haha hoho +�?�?�
PS : I do not need access properties file, this is just test-case, to be sure, that the file is in correct format - therefore working as expected with @Value("${hacky.carky}")
The response header is same in both cases, having this property
Content-Type:application/json;charset=UTF-8
I do have filter an filter-mapping for utf-8 set in web.xml :
<filter>
<filter-name>encoding-filter</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>encoding-filter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
I do have utf-8 setting in my pom.xml for maven :
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
The example of full address is http://localhost:8080/czechtraditions-server/rest/aserver/regions
Solved.
I do not convert the file to String, I send the byte array and let client to decide (based on content-type in header) what to do with it and it works fine.
@RequestMapping(value = "/regions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object regions(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
String filePath = "c:\\prace\\eclipse workspace\\czechtraditions\\server\\src\\main\\resources\\server-general.properties";
return Files.readAllBytes(Paths.get(filePath));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.