Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - Spring MVC response encoding issue

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 of that, quite simple :

@ResponseBody
    @RequestMapping(value="test")
    public String test(){
        String test = "?????";
        System.out.println(test);
        logger.info(test);
        return test;
    }

This is my web.xml, because I found some answers where CharacterEncodingFilter helped(not in my case though). I used POST method because I read this applies to POST.

Also found this answer(related). Didn't help as well.

When I debug it the correct value appears, but when I print it doesn't as it can be seen below:

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...