Hello. Trying to do this:

@RequestMapping(value = "/something") @ResponseBody public String helloWorld() { return "Русский текст"; } 

Page takes about this:

 ??????? ????? 

On the page encoding UTF-8. But even if she were different, in any case there would be no question marks. Please advise how to correct the situation.

I will add the question: It turned out very strange in this situation:

 @RequestMapping(value = "/something") public ResponseEntity<MyCls> helloWorld() { MyCls cls = new MyCls(); cls.setStr("Русский текст"); HttpHeaders h = new HttpHeaders(); return new ResponseEntity(cls, h, HttpStatus.OK); } 

Now it worked the way I expected:

 {"str":"Русский текст"} 

Do you have a spring for allergy type String?

    6 answers 6

    It seems that this is a known problem (a similar issue on SO , a bug in Jire ), it is solved in several ways:

    • as indicated by @Nofate - use ResponseEntity with appropriate headers;
    • add a converter to hw-servlet.xml (code below);
    • zababahat your converter with blackj ... (as here ).

    Configured converter:

     <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <array> <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </bean> </array> </property> </bean> 

    The response to SO says that the second method will not work with mvc:annotation-driven - I don’t know, it worked for me and with it.


    UPD. Yeah, with mvc:annotation-driven will not work if you put the converter after this announcement.

    • I actually dug all the same, but with converters I have something that does not grow into practice. - Nofate
    • SUPER! Thank you very much! - Anton Mukhin
    • @ Anton Mukhin, if you have Hello World with the above-mentioned converters, will it be easy for you to drop me in the mail? I want to understand the question to the end, but I don’t go out on my own - Nofate
    • The letter went ... - Anton Mukhin

    In the web.xml add the filter itself:

     <!--encoding filter--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

    In the database connection settings, add the following code:

     jdbc:mysql://localhost:3306/dataBaseName<b>?characterEncoding=UTF-8</b> 

      Check just in case the encoding of the source file itself. Question marks are a sure sign of trying to interpret a cp1251 string as a UTF string.

      -

      UPD1. All that has happened so far is:

       public ResponseEntity<String> preview(HttpServletResponse response) { HttpHeaders h = new HttpHeaders(); h.add("Content-type", "text/html;charset=UTF-8"); return new ResponseEntity<String>("Привет мир",h ,HttpStatus.OK); } 
      • ??? ?????? - this is the result of the program after changing the source file to UTF-8. Those. slightly changed, but there is no positive result. URIEncoding I have UTF-8. - Anton Mukhin
      • If there is a possibility, I would suggest that you put the project on some file storage so that you can see what is there and how. - Nofate
      • Hm The project is very big. If you do not mind, I will do a small healoword, and send you a project in the archive by mail - Anton Mukhin

      I will add more from myself. If you use error handling @ExceptionHandler and you need Russian text, for example in sql errors from the database on the front and transfer them for example in json format, then you also need to add to ..- servlet.xml:

       <bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"> <property name="messageConverters"> <array> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg index="0" name="defaultCharset" value="UTF-8"/> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value> </list> </property> </bean> </array> </property> </bean> 

      Well, the maven dependencies:

        <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency> 

      I confirm, works with <mvc:annotation-driven/> , declared after the bins. Maybe someone will come in handy. My post on this topic

        If Tomcat is used, then in server.conf (I think this is the name of the file), you need to fix the connector you are working on - add URIEncoding="UTF-8" .

        • I have GlassFish. I will try to find now how to change it there. - Anton Mukhin
        • No, it did not help. - Anton Mukhin
        • If the jsp page usually helps this <% @ page session = "false" isELIgnored = "false" pageEncoding = "UTF-8" contentType = "text / html; charset = UTF-8"%> - barmaglott

        In the application descriptor, you need to add a filter:

         <filter> <filter-name>encodingFilter</filter-name> <filterclass> org.springframework.web.filter.CharacterEncodingFilter</filterclass> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> 
        • Yes, and it would be nice to see the ViewResolver definition. - a_gura
        • Also incorrect ( - Anton Mukhin
        • @Anton, did you filter the filter specified by @a_gura? Like this: <filter-mapping> <filter-name> encodingFilter </ filter-name> <url-pattern> / * </ url-pattern> </ filter-mapping> - yozh
        • Nope, I'll try now. All right! I'm in a complete stupor! Apparently, either I have a wonderful computer, or Spring really has a String class, in which the text is Cyrillic, allergic !!! And with <filter-mapping> questions also come to the page. Here, apparently, the problem is not in the server, and not in the web.xml file. Here, apparently, the problem with the spring. Maybe some additional settings need to be done? It seems I did everything according to the textbook, everything is correct. Created a new project to check what happens. The results are the same. - Anton Mukhin
        • Can you email me a test project? - yozh