好的,所以我尝试了所有方法,但在stackoverflow上询问...
我正在尝试使用来自 Android 的一些 http 参数执行 REST 调用,使用 httpclient 和 resttemplate 到服务器端 Spring 控制器。我所有的瑞典语字符最终都以“\u001A”的形式出现在服务器上......
设置 httpclient 和 resttemplate 代码:
HttpClient httpClient = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials(msisdn, password);
httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
//httpClient.getParams().setContentCharset(prefs.());
httpClient.getParams().setCredentialCharset(prefs.getCredentialsEncoding());
CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
// Add message converters
List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
json.setSupportedMediaTypes(supportedMediaTypes);
mc.add(json);
restTemplate.setMessageConverters(mc);
return restTemplate;
然后我用我的参数准备一个httpentity:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("myparam", "" + "å");
HttpEntity<String> entity = new HttpEntity<String>(headers);
我终于打了剩下的电话:
ResponseEntity<UserStatusTO> response = rest.exchange(url, HttpMethod.GET, entity,
MyResponseClass.class);
在服务器上,我有一个杰克逊反序列化器,我的弹簧控制器方法如下所示:
@RequestMapping(method = RequestMethod.GET, value = "/event/out", headers = "Accept=application/json", produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
public
@ResponseBody
UserStatusTO out(Authentication auth, @RequestHeader(value="myparam", required = false) String myparam) {
所有特殊字符都以 \u001a 结尾!我已经尝试了很多东西,手动重新编码字符串客户端和服务器端,没有一个工作。我试过摆弄 httpClient.getParams().setContentCharset(); httpClient.getParams().setUriCharset();
据我所知,没有一个有效。
我没主意了!如果有人有任何意见,我将不胜感激。谢谢!