1

When I try to output string in Java like this:

System.output.println("Привет");

Console output shows me this result:

Привет

I have a REST API method where I receive string from outside request. When I send exact same Привет string with UTF-8 encoding and try to output it like this:

post("/check", (req, res) -> {
    receivedString = req.body();
}

System.ouput.println(receivedString);

It shows this:

������

What I need to do in order to turn this questionmark thing into proper readable string?

Mr.D
  • 7,353
  • 13
  • 60
  • 119
  • Looking in stackoverflow, i found any what can help you: https://stackoverflow.com/questions/28567208/how-can-i-change-the-standard-out-to-utf-8-in-java/42957623 – Adrian Lagartera Jul 12 '21 at 18:01
  • @AL thanks for help, but these answers are trightly coupled into output stream which is not very useful, because I want to use this string in some other operations. – Mr.D Jul 12 '21 at 18:11
  • “I have a REST API method where I receive string” … Edit your question and show that method’s signature, and the code that obtains the String from the request. I suspect the code is assuming the wrong charset. – VGR Jul 12 '21 at 21:09
  • @VGR Added method how I get string – Mr.D Jul 13 '21 at 04:11
  • Use the debugger to check whether `receivedString` is correct or not. This will tell you if the problem is on the input (POST request) or output side. If you are on Windows, do not trust standard output (`System.out`). It is unlikely that it is configured to handle UTF-8. – Codo Jul 13 '21 at 08:34
  • What is the type of `req`? What class defines the `post` method? – VGR Jul 13 '21 at 12:34

1 Answers1

-2

You can try with ...

PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(receivedString);
GURU Shreyansh
  • 881
  • 1
  • 7
  • 19