Spring MVC response encoding issue

前端 未结 4 1657
醉酒成梦
醉酒成梦 2020-12-10 21:09

In last few hours I\'ve read a lot concerning this topic, and so far nothing has worked. I\'m trying to return response containing \"odd\" some characters. Here is example o

相关标签:
4条回答
  • 2020-12-10 21:34

    After few days of this I just had "who's your daddy moment". It came from reading spring 3.0 reference, I had nothing else to try so why not go trough entire documentation.. and combination of @axtavt answer :

    Who sets response content-type in Spring MVC (@ResponseBody)

    Changed original solution :

    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;
        }
    

    To :

    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", "plain", 
                                Charset.forName("UTF-8"))));
                    }
                }
            }
            return bean;
        }
    

    Darn spring!!! but still I'll continue to use it.

    0 讨论(0)
  • 2020-12-10 21:36

    Instead @ResponseBody use ResponseEntity.

    @RequestMapping(value="test")
    public ResponseEntity<String> test(){
        String test = "čćžđš";
        System.out.println(test);
        logger.info(test);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=UTF-8");
        return ResponseEntity<String>(test,responseHeaders, HttpStatus.OK);
    }
    
    0 讨论(0)
  • 2020-12-10 21:40

    I can see two problems in the actual delivered response.

    • The response is clearly just text, but your response content-type header is saying it is HTML.

    • Judging from the content-length of the response, the content has not actually been encoded in UTF-8.


    FWIW - the CharacterEncodingFilter won't help with your problem because it is dealing with the encoding of the request not the response.


    I think that the problem is that you need to configure the message converter for the response body. However, it appears that your application already does something in this area, because the default behavior of the StringHttpMessageConverter is to use "text/plain" as its content type.

    0 讨论(0)
  • 2020-12-10 21:46

    My simple solution:

    @RequestMapping(value="test")
    public ModelAndView test(){
      String test = "čćžđš";
      ...
      ModelAndView mav = new ModelAndView("html_utf8");
      mav.addObject("responseBody", test);
    }
    

    and the view html_utf8.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>${responseBody}
    

    No additional classes and configuration.
    And You can also create another view (for example json_utf8) for other content type.

    0 讨论(0)
提交回复
热议问题