【问题标题】:Wrong encoding with GWT, restygwt, Spring MVC, JSON. ISO-8859-1 vs. utf-8?GWT、restygwt、Spring MVC、JSON 编码错误。 ISO-8859-1 与 utf-8?
【发布时间】:2012-07-09 09:21:46
【问题描述】:

我真的不知道问题出在哪里,但我可以告诉以下内容:

首先我使用了 GWT RequestBuilder:

RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/myRESTResource/test");
rb.setHeader("Content-Type", "application/json");
rb.setRequestData("");
rb.setCallback(new RequestCallback() {
    @Override
    public void onResponseReceived(Request request, Response response) {
        Window.alert(response.getText());
    }
    @Override
    public void onError(Request request, Throwable exception) {
        Window.alert(exception);
    }
});
rb.send();

我的 Spring 控制器:

@Controller
@RequestMapping("/myRESTResource")
public class TranslationController {
    @RequestMapping(value="/{param}", method=RequestMethod.GET)
    public @ResponseBody String get(@PathVariable("param") String param, HttpServletResponse response) {
        // get some data and output it as JSON
    }
}

在 Chrome 浏览器中,我可以看到以 Content-type: text/html; charset=iso-8859-1 返回的响应。 Window.alert() 很好。

现在我想切换到 restygwt。我正在使用Resource

Resource r = new Resource("myRESTResource/test");
r.get().send(new JsonCallback() {
    @Override
    public void onSuccess(Method method, JSONValue response) {
        Window.alert(response.toString());
    }

    @Override
    public void onFailure(Method method, Throwable exception) {
        Window.alert(exception.getLocalizedMessage());
    }
});

这里我的 Spring 控制器返回 Content-type: application/jsonWindow.alert() 适用于 ASCII 字符,但所有特别的东西都变成了一个黑盒子,里面有一个问号。该问题出现在 GWT 开发模式(带有集成 Jetty 的 Eclipse 4.2)中,并且还部署在外部 Tomcat 7 上。

如果我使用 restygwt 并放

response.setCharacterEncoding("ISO-8859-1");

在我的 Spring Controller 中,Window.alert() 按预期显示数据。

很明显,我不想在每个 Controller 方法中都写 response.setCharacterEncoding("ISO-8859-1");

我希望有一个简单的方法来解决这个问题。谢谢。

【问题讨论】:

    标签: json gwt rest spring-mvc utf-8


    【解决方案1】:

    解决方法很简单:

    只要说restygwt Resource它想从服务器获取哪种编码:

    Accept: application/json; charset=utf-8

    或:

    Accept: application/json; charset=iso-8859-1

    代码示例:

    HashMap<String, String> header = new HashMap<String, String>();
    header.put(Resource.HEADER_ACCEPT, Resource.CONTENT_TYPE_JSON+"; charset=utf-8");
    
    Resource r = new Resource("myRESTResource/test", header);
    r.get().send(new JsonCallback() {
        @Override
        public void onSuccess(Method method, JSONValue response) {
            Window.alert(response.toString());
        }
    
        @Override
        public void onFailure(Method method, Throwable exception) {
            Window.alert(exception.getLocalizedMessage());
        }
    });
    

    Spring 会自动返回预期的编码!

    【讨论】:

      【解决方案2】:

      你似乎没有从服务器生成 json。 你只是返回一个字符串。

      尝试将其放入您的服务中:

      @RequestMapping(value="/{param}", method=RequestMethod.GET**, produces = "application/json"**)

      您可以使用 RestClient 之类的工具来验证服务器是否确实返回了 json。

      【讨论】:

      • 如您所见,我已经回答了我自己的问题;-) 解决方案非常简单:Spring 将返回您的请求。这意味着如果您的 HTTP 请求标头指定您想要接收 application/json,那么 Spring 会这样做。但是您的解决方案也可以。
      猜你喜欢
      • 2016-10-04
      • 2012-05-27
      • 2011-09-23
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多