Results 1 to 2 of 2

Thread: Spring MVC encoding problems

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    Exclamation 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •