-2

I'm trying to get data with Latin characters in it which is like ('ş','ç','ğ','ı','İ','Ü') but instead what i'm getting as output is 'Ä�' for 'ğ', 'ç' for 'ç' Whatever i try the output is weird.

 CASE WHEN type = 'a' THEN 'ğ ç ş'
 ELSE 'null' END AS description

The part of sql that i'm having trouble with is like as seen above. The problem is in that sql part when i logged my endpoint value of description looks weird from the very beginning.

Repository class which returns query values is like below.

public LPModel findLPInfo(String id) {
    LPModel list = jdbcTemplate
            .queryForObject(QueryConstants.GET_PL_INFO, new Object[]{id}, new LMapper());
    return list;
} 

My hibernate properties is below. I added charset details to fix my problem but that didn't work.

spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:databaseName?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8
@PostMapping(value = "/getid", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<PostResponse> getid(@RequestBody LWRequest postRequest {

PostResponse response= new PostResponse();

LPModel lpm = listRepository.findLPInfo(postRequest.id);

String desc = String.valueOf(new String(lpm.DESCRIPTION.getBytes(), "UTF-8").getBytes("ISO-8859-1"));
log.info("Description " + lpm.DESCRIPTION +" "+ desc);// desc and lpm.DESCRIPTION returns like '�'

response.setDESC(lpm.DESCRIPTION);

return response;
}

End point is working and i added produces = MediaType.APPLICATION_JSON_UTF8_VALUE part just to be sure that response is in UTF-8 type and with desc i tried to encode to utf-8 but that didn't worked either. i'm only showing relevant parts of my code about my problem.

i need to show output with exact characters like ğ ç ş

  • 2
    Why are you converting encodings and then putting it back in a String? It's only possible to lose data by doing that. – Louis Wasserman Jun 02 '22 at 22:28
  • What do you suggest me to do? I did like at the examples that i seen when searching for the answer. Like (https://stackoverflow.com/questions/652161/how-do-i-convert-between-iso-8859-1-and-utf-8-in-java) – Özgür Akdeniz Jun 02 '22 at 22:35
  • That's from bytes to bytes, not string to string. `String desc = lpm.DESCRIPTION` is the right call for that part. (Though that's very strange style -- is `DESCRIPTION` a static constant?) – Louis Wasserman Jun 02 '22 at 22:58
  • DESCRIPTION defined as public String DESCRIPTION in the LPModel class with lombok getter and setter annotations – Özgür Akdeniz Jun 02 '22 at 23:04
  • You're not putting `desc` in the response. You're logging it, then throwing it away. Your response description is just `lpm.DESCRIPTION`. – Dawood ibn Kareem Jun 02 '22 at 23:09
  • Yes, that's right. I didnt show it in response because it didn't returned anything different in the log.info than lpm.DESCRIPTION. – Özgür Akdeniz Jun 02 '22 at 23:13
  • It's a simple [mojibake](https://en.wikipedia.org/wiki/Mojibake) case (example in Python for its common intelligibility): `'ş ç ğ ı İ Ü'.encode( 'utf-8').decode( 'cp1254')` returns `ÅŸ ç ÄŸ ı İ Ü` – JosefZ Jun 03 '22 at 12:34

0 Answers0