0

I am trying to hit request from postman consisting of arabic characters:

{ "data": "{\"holder passport expiry date\":\"10/09/2021\",\"holder passport issue date\":\"11/09/2011\",\"holder sponsor name\":\"رائد ابو زيدsss\",\"holder occupation code\":8}" }

While I am trying to print the request in console using logger.info or System.out.println like this:

public void printRecord(RequestBodyy requestBody) throws UnsupportedEncodingException, JSONException {
    JSONObject jsonObject = new JSONObject(requestBody.getData());
    System.out.println(jsonObject);
    
    System.out.print(jsonObject.get("holder passport expiry date"));
    
    String str2 = new String("رائد ابو زيدsss");//.getBytes(), "UTF-8");
    System.out.println(str2);
    String str = new String(jsonObject.get("holder sponsor name").toString());//.getBytes(),"UTF-8");
    System.out.println(str);
    
    String data = "{\"eidaID\":\"حسن\"}"; JSONObject obj = new JSONObject(data); 
    System.out.println(obj.get("eidaID").toString());
}

Output:

{"holder sponsor name":"???? ??? ???sss","holder passport expiry date":"10/09/2021","holder occupation code":8,"holder passport issue date":"11/09/2011"}
10/09/2021???? ??? ???sss
???? ??? ???sss
???

I am receiving ???? instead of those arabic characters.

How will I able to do that ?

However, I am able to store these characters in DB coming from API request.

My application.properties :

server.port = 8076
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
server.tomcat.uri-encoding=UTF-8

In pom.xml, I added:

<properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Please help!

1 Answers1

0

try to convert it to a UTF-8 compatible string and then try to print it in console

String str = new String("رائد ابو زيدsss".getBytes(), "UTF-8");

now print this string in console or log. You can replace the Arabic string as per your input

===========================================================

UPDATE 1

I have done a sample example based on the code you have given. You may change the request processing part as per your need.

Make sure you have changed the text file encoding for your project to UTF-8. I have used STS (you will get it in Project > Properties)

enter image description here

Code

    @RestController
    public class MainController {
        
        @GetMapping(value="/test", consumes = "application/json", produces ="application/json" )
        public void getData(@RequestBody String body) throws 
UnsupportedEncodingException { // I have accepted it as String, you can accept as per your requirement like any Object
            System.out.println("Request Body: " +body);
            JSONObject jsonObject = (JSONObject) JSONValue.parse(body); //This is just to replicate your code
            System.out.print("holder sponsor name: " +jsonObject.get("holder sponsor name"));
        }
    
    }

Request

enter image description here

Result

  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.6.0)[0;39m

Request Body: {
    "holder passport expiry date": "10/09/2021",
    "holder passport issue date": "11/09/2011",
    "holder sponsor name": "رائد ابو زيدsss",
    "holder occupation code": 8
}
holder sponsor name: رائد ابو زيدsss
newcoder
  • 464
  • 9
  • 23