在我的项目中,我分离了后端模块和前端模块,并通过从后端提供REST api来运行,并使用Apache Http Client和GSON对其进行调用。
我想在UI(网页)上提供多种语言,例如德语,法语...。
在网页上,它显示为“SchlössliSchönegg,Wilhelmsh��he”,但在数据库和RestAPI中,json是“SchlössliSchönegg”。
如何支持多国语言?
在后端,我编写了诸如get,put,post之类的Request方法,在前端,我使用HttpClient和GSON将JSON转换为Java Object。
我在html内尝试过,但主要问题是GSON在将它从fromJSON()转换时,原始JSON值““SchlössliSchönegg”变成了“Schl��ssliSch��negg,Wilhelmsh��he”。
在RestAPI中,JSON数据为
{
“ addressId”:3,
“ buildingName”:“SchlössliSchönegg”,
“街道”:“Wilhelmshöhe”,
“ ward”:“ 6003”,
“ district”:“ luzern”,
“ cityOrProvince”:“卢塞恩州”,
“国家”:“瑞士”
}
但是在前端,GSON转换后的Java对象字符串数据是
(..buildingName =SchlössliSchönegg,街道=Wilhelmshöhe,病房= 6003,区=卢塞恩,cityOrProvince =卢塞恩州,国家/地区=瑞士)
在这里,RestClient功能代码
public List<FakeEmployeeDTO> getAllEmployeeList() throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(URL);
HttpResponse response = client.execute(getRequest);
Integer statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new SystemException(ERROR_MESSAGE + statusCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuilder jsonData = new StringBuilder();
while ((line = rd.readLine()) != null) {
jsonData.append(line);
}
response.getEntity().getContent().close();
rd.close();
logger.info(jsonData.toString());
Gson gson = new GsonBuilder().setDateFormat("dd-MMM-yyyy").create();
Type listType = new TypeToken<List<FakeEmployeeDTO>>() {
}.getType();
List<FakeEmployeeDTO> employeeList = gson.fromJson(jsonData.toString(), listType);
sortEmployeeListByFirstName(employeeList);
return employeeList;
}
在Employee内部,我有地址atrribute,在该地址内我具有诸如buildingNumber和Street的值,该值可以是任何语言。
最佳答案
试试这个
(BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));)
整个代码最终将像这样。
public List<FakeEmployeeDTO> getAllEmployeeList() throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest);
Integer statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new SystemException(ERROR_MESSAGE + statusCode);
}
BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(),
"UTF-8")); // if it is not working please try with ("ISO-8859-1")
String line = "";
StringBuilder jsonData = new StringBuilder();
while ((line = rd.readLine()) != null) {
jsonData.append(line);
}
response.getEntity().getContent().close();
rd.close();
logger.info(jsonData.toString());
Gson gson = new GsonBuilder().setDateFormat("dd-MMM-yyyy").create();
Type listType = new TypeToken<List<FakeEmployeeDTO>>() {
}.getType();
List<FakeEmployeeDTO> employeeList = gson.fromJson(jsonData.toString(), listType);
sortEmployeeListByFirstName(employeeList);
return employeeList;}
下次请务必尝试使用此方法,因为您可以使用“尝试使用资源”
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new URL("https://htt.your url.com" + URLEncoder.encode(query, "UTF-8") )
.openConnection().getInputStream()))) {