objectMapper将hindi文本转换为特殊字符“???”

时间:2017-07-30 08:36:07

标签: java spring-mvc objectmapper

我将印地语存储在数据库中。 在我的fetch方法中,我正在使用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
    }
]

2 个答案:

答案 0 :(得分:1)

刚刚将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);
            }
        }

它解决了我的问题。

答案 1 :(得分:0)

您可以启用漂亮打印配置杰克逊映射器缩进:mapper.enable(SerializationFeature.INDENT_OUTPUT);,这应该可以正确进行转换。

检查documentation是否有Seri​​alizationFeature:

  

允许启用(或禁用)缩进的功能   底层生成器,使用默认的漂亮打印机(参见   JsonGenerator.useDefaultPrettyPrinter()了解详情。)