-1

We Are trying to store non ascii character(French and Arabic) in mongodb.

Its getting saved in mongodb correctly but while retrieving using repository.findAll() function of spring-data-mongo we are getting "??????" for all non ascii characters.

Till now tried Goolge,Stackoverflow

mongo-cli is giving correct result

upgraded mongo version

On localhost its working fine somehow but on development machine its giving wrong values thought both have same code and mongodb version or operating system is different

  • Mongo stores all data in UTF-8, you cannot change that. Obviously the problem is somewhere in your spring, not at mongo. – Wernfried Domscheit Oct 15 '20 at 14:48
  • i am still trying to identify problem. but same spring version code is working on 1 machine and not working on another. Although mongo is storing correctly and cli is giving correct response . but while fetching Spring-data-mongodb giving diffrent output on diffrent machines – pramod bhargava Oct 16 '20 at 10:13
  • Maybe have a look at https://stackoverflow.com/q/39163590 – Wernfried Domscheit Oct 17 '20 at 10:19
  • @WernfriedDomscheit that worked Thanks. implemented filter to change request and response to UTF-8 – pramod bhargava Oct 19 '20 at 12:42

1 Answers1

0

This problem is related to Spring boot and utf 8 encoding on linux machine.

This Answer explains complete problem How to set UTF-8 character encoding in Spring boot?

To fix this and remove dependency from machine added filter in application

public class CharactersSetFilter implements Filter {

// ...

public void doFilter(
  ServletRequest request, 
  ServletResponse response, 
  FilterChain next) throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
  //  response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    next.doFilter(request, response);
}

}

This fixed the problem