Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC encoding problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC encoding problems

    I've been trying to solve this issue for days now, I hope someone here had similar issue, what I try to do isn't really that complex, it should be pretty straight forward, I'm trying to return "non" standard characters in response, ex :

    Code:
    @ResponseBody
        @RequestMapping(value="test")
        public String test(){
            String test = "čćđ";
            System.out.println(test);
            logger.info(test);
            return test;
        }
    When I debug it the correct value appears, but when I print it doesn't as it can be seen in the image below:

    http://i.stack.imgur.com/cNegv.jpg

    When I test it from jmeter, the response seems to be OK, Content-Type is text/html;charset=UTF-8

    Here is a screenshot of that as well. http://i56.tinypic.com/14lt653.jpg

    I think the right way is to return UTF-8, maybe I'm wrong.

    Also I've tried this :

    Code:
    <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>
    Code:
    public class EncodingPostProcessor implements BeanPostProcessor {
        public Object postProcessBeforeInitialization(Object bean, String name)
                throws BeansException {
            if (bean instanceof AnnotationMethodHandlerAdapter) {
                HttpMessageConverter<?>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();
                for (HttpMessageConverter<?> conv: convs) {
                    if (conv instanceof StringHttpMessageConverter) {
                        ((StringHttpMessageConverter) conv).setSupportedMediaTypes(
                            Arrays.asList(new MediaType("text", "html", 
                                Charset.forName("UTF-8"))));
                    }
                }
            }
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String name)
                throws BeansException {
            return bean;
        }
    }
    Code:
    <bean class = "EncodingPostProcessor " />
    But it doesn't seem to do the trick, please help.

  • #2
    The initial string you create is that in UTF8? If it isn't you might run into trouble converting it.

    Also is the encoding in/of your JSP in UTF8 and not only the response from the controller?

    Comment

    Working...
    X