Search code examples
javaspring-mvcobjectmapper

objectMapper converting hindi text into special charactor "???"


I'm storing Hindi in database. In my fetch method I'm using objectMapper which is converting Hindi font into special character. without objectmapper it's working fine.

@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
    }
]

but it should be:

[
    {
        "department": "जल कल विभाग",
        "departmentId": 1
    }
]

Solution

  • just changed mediaType to 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);
                }
            }
    

    it resolved my problem.