Reputation: 3981
Ok, so ive tried everything but asking at stackoverflow...
I'm trying to perform a REST call with some http params from an Android using httpclient and resttemplate to a server-side Spring controller. All my Swedish chars end up on the server as '\u001A'...
setting up httpclient and resttemplate code:
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;
I then prepare a httpentity with my parameter:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("myparam", "" + "å");
HttpEntity<String> entity = new HttpEntity<String>(headers);
I finally make the rest call:
ResponseEntity<UserStatusTO> response = rest.exchange(url, HttpMethod.GET, entity,
MyResponseClass.class);
On the server, i have a jackson deserializer and my spring controller method looks like:
@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) {
All the special chars end up as \u001a ! I've tried tons of stuff, re-encoding the strings manually client AND server side, none worked. I've tried fiddling with the httpClient.getParams().setContentCharset(); httpClient.getParams().setUriCharset();
None worked as far as i could tell.
I'm out of ideas! If anybody has any input, i'd be much obliged. Thanks!
Upvotes: 0
Views: 2670
Reputation: 616
I had a similar issue and I fixed it by setting the proper content-type AND character set manually instead of using MediaType.APPLICATION_JSON:
HttpHeaders headers = new HttpHeaders();
Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType("application", "json", utf8);
headers.setContentType(mediaType);
Upvotes: 4
Reputation: 17087
This sounds like an issue on the server side since the Apache HTTP client will gladly output Latin-1 characters in headers, as you can test with this simple test:
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 is a little hack to use the AbstractSessionOutputBuffer
class SessionOutputBufferImpl extends AbstractSessionOutputBuffer {
SessionOutputBufferImpl(OutputStream out, HttpParams params) {
init(out, 1024, params);
}
}
You should probably concentrate on your server backend - what are you using? If it's "fixed" - you should consider trying to format your header according to RFC 2407 - some servers support, others fail miserably.
Upvotes: 1