2017-07-30 60 views
1

我在數據庫中存儲印地語。 在我的獲取方法中,我使用objectMapper將印地文字體轉換爲特殊字符。沒有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 
    } 
] 
+0

你確定這個腐敗發生在序列化到字符串期間,而不是在讀取輸入期間?首先使用調試器檢查'departmentList'包含正確的值。 – AlexR

+0

當我刪除objectmapper它會返回預期的結果。所以是的,它與objectmapper的問題。 –

回答

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

它解決了我的問題。

0

您可以啓用漂亮的打印配置壓痕您傑克遜映射:mapper.enable(SerializationFeature.INDENT_OUTPUT);這應該做的轉換正確。

檢查documentation爲SerializationFeature:

功能,允許啓用(或禁用)縮進的 底層發生器,使用默認的漂亮打印機(見 JsonGenerator.useDefaultPrettyPrinter()的詳細信息)。