Results 1 to 8 of 8

Thread: Problem encode with @ResponseBody

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Default Problem encode with @ResponseBody

    I'm using Spring/Roo for a web app, and when i make some return with the @ResponseBody the Specifically, characters like "ñ á é í "... the response appear as "?". How can they be properly encoded ?
    am using spring 3.1.1. hope some one can help thx in advance.

  2. #2
    Join Date
    Mar 2007
    Posts
    566

    Default

    Your response encoding does not match the encoding sent in the Response header.

  3. #3
    Join Date
    Jul 2012
    Posts
    4

    Default

    how can i set the response encoding?, but am not sure if that is the problem, because if y just print the string "ñ á é í" the result is
    ? ? ? ? , dont know if roo put a diferent encoding or in the pom.xml is missing something.

  4. #4
    Join Date
    Mar 2007
    Posts
    566

    Default

    Show us the controller method which writes the response.

    You can set the response header in several ways...
    In the html page via meta tag or via response.setHeader("Content-Type", ...) or response.setCharacterEncoding(..) and so on.

  5. #5
    Join Date
    Jul 2012
    Posts
    4

    Default

    this is the method

    @RequestMapping(value = "/maptest", method = RequestMethod.GET)
    public @ResponseBody
    String maptest() {
    System.out.println("á ñ é");
    String respuesta="á ñ é";
    return respuesta;
    }

    /// also i tried this in the method

    @RequestMapping(value = "/maptest", method = RequestMethod.GET)
    public ResponseEntity<String> maptest() {
    String respuesta="á ñ é";
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "text/html;charset=UTF-8");
    return new ResponseEntity<String>(respuesta, responseHeaders, HttpStatus.CREATED);
    }

    with both the result was the same " ? ? ? " interrogation symbols

  6. #6
    Join Date
    Dec 2010
    Posts
    29

    Default

    Yon could try org.apache.commons.lang.StringEscapeUtils.escapeHt ml()

    @RequestMapping(value = "/maptest", method = RequestMethod.GET)
    public @ResponseBody String maptest() {
    System.out.println("á ñ é");
    String respuesta="á ñ é";
    return StringEscapeUtils.escapeHtml(respuesta);
    }

  7. #7
    Join Date
    Mar 2007
    Posts
    566

    Default

    Just look into the response in your browser and inspect the headers and meta-tags (F12 helps).

  8. #8
    Join Date
    Jul 2012
    Posts
    4

    Default

    the headers are set in UTF-8, that is no the problem, The server has no way of knowing how to interpret the special characters, even if i just print this System.out.println("á ñ é"); the console show ? ? ?, an i have the IDE set in UTF-8, the file set in UTF -8,but does not work, if i create a simple maven project the special characters work, but with spring - roo, with the example of the pizzashop, does not work the special characters, what is missing in the configuration?

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
  •