好的,所以我尝试了所有的东西,只是在stackoverflow上询问......
我正在尝试使用一些来自Android的http params使用httpclient和resttemplate执行REST调用到服务器端Spring控制器。我所有的瑞典语字符都以“\ u001A”...
的形式出现在服务器上设置httpclient并重新设置代码:
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);
在服务器上,我有一个jackson反序列化器,我的弹簧控制器方法如下:
@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();
据我所知,没有任何工作。
我没有想法!如果有人有任何意见,我会非常感激。谢谢!
答案 0 :(得分:4)
我有类似的问题,我通过手动设置正确的内容类型AND字符集而不是使用MediaType.APPLICATION_JSON来修复它:
HttpHeaders headers = new HttpHeaders();
Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType("application", "json", utf8);
headers.setContentType(mediaType);
答案 1 :(得分:1)
这听起来像是服务器端的问题,因为Apache HTTP客户端很乐意在头文件中输出Latin-1字符,因为您可以使用这个简单的测试进行测试:
HttpGet get = new HttpGet("http://www.riksdagen.se");
get.addHeader("SwedishHeader", "Mona Sahlin är ett nötdjur");
ByteArrayOutputStream out = new ByteArrayOutputStream();
SessionOutputBufferImpl buffer = new SessionOutputBufferImpl(out, get.getParams());
HttpRequestWriter writer = new HttpRequestWriter(buffer, BasicLineFormatter.DEFAULT, get.getParams());
writer.write(get);
buffer.flush();
System.out.println(out.toString("ISO-8859-1"));
SessionOutputBufferImpl是一个使用AbstractSessionOutputBuffer的小黑客
class SessionOutputBufferImpl extends AbstractSessionOutputBuffer {
SessionOutputBufferImpl(OutputStream out, HttpParams params) {
init(out, 1024, params);
}
}
您应该专注于您的服务器后端 - 您在使用什么?如果它是“固定的” - 您应该考虑根据RFC 2407尝试格式化标题 - 某些服务器支持,其他服务器失败。