objectMapper 将印地文文本转换为特殊字符“???”
objectMapper converting hindi text into special charactor "???"
我正在将印地语存储在数据库中。
在我的获取方法中,我使用 objectMapper
将印地语字体转换为特殊字符。没有 objectmapper
它工作正常。
@CrossOrigin
@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> fetchDepartmentInfo() {
try {
List<Map<String, Object>> departmentList = departmentServices.fetchDepartments();
if (departmentList == null || departmentList.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<String>(new ObjectMapper().writeValueAsString(departmentList), HttpStatus.OK);
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
o/p:
[
{
"department": "?? ?? ?????",
"departmentId": 1
}
]
但应该是:
[
{
"department": "जल कल विभाग",
"departmentId": 1
}
]
您可以启用漂亮的印刷配置缩进您的 Jackson 映射器:mapper.enable(SerializationFeature.INDENT_OUTPUT);
这应该可以正确进行转换。
正在检查 documentation 的序列化特征:
Feature that allows enabling (or disabling) indentation for the
underlying generator, using the default pretty printer (see
JsonGenerator.useDefaultPrettyPrinter() for details).
刚刚将 mediaType 更改为 APPLICATION_JSON_UTF8_VALUE
@CrossOrigin
@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<?> fetchDepartmentInfo() {
try {
List<Map<String, Object>> departmentList = departmentServices.fetchDepartments();
if (departmentList == null || departmentList.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<String>(new ObjectMapper().writeValueAsString(departmentList), HttpStatus.OK);
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
它解决了我的问题。
我正在将印地语存储在数据库中。
在我的获取方法中,我使用 objectMapper
将印地语字体转换为特殊字符。没有 objectmapper
它工作正常。
@CrossOrigin
@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> fetchDepartmentInfo() {
try {
List<Map<String, Object>> departmentList = departmentServices.fetchDepartments();
if (departmentList == null || departmentList.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<String>(new ObjectMapper().writeValueAsString(departmentList), HttpStatus.OK);
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
o/p:
[
{
"department": "?? ?? ?????",
"departmentId": 1
}
]
但应该是:
[
{
"department": "जल कल विभाग",
"departmentId": 1
}
]
您可以启用漂亮的印刷配置缩进您的 Jackson 映射器:mapper.enable(SerializationFeature.INDENT_OUTPUT);
这应该可以正确进行转换。
正在检查 documentation 的序列化特征:
Feature that allows enabling (or disabling) indentation for the underlying generator, using the default pretty printer (see JsonGenerator.useDefaultPrettyPrinter() for details).
刚刚将 mediaType 更改为 APPLICATION_JSON_UTF8_VALUE
@CrossOrigin
@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<?> fetchDepartmentInfo() {
try {
List<Map<String, Object>> departmentList = departmentServices.fetchDepartments();
if (departmentList == null || departmentList.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<String>(new ObjectMapper().writeValueAsString(departmentList), HttpStatus.OK);
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
它解决了我的问题。