【发布时间】: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/json。 Window.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