Search code examples
javautf-8apache-httpclient-4.xgeoserver

Getting results of WFS request to GeoServer as UTF-8


I have the following code to send a WFS request to a locally running GeoServer instance using the Apache http-client-4.1 library:

        ThreadSafeClientConnManager connectionMngr = new ThreadSafeClientConnManager();
        DefaultHttpClient httpClient = new DefaultHttpClient(this.connectionMngr);
        HttpPost httpPost = new HttpPost(wfsUrl);
        httpPost.setEntity(new StringEntity(wsft, HTTP.UTF_8));

        log.debug("Submitting WSFT request: " + wsft);

        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String result = httpClient.execute(httpPost, responseHandler);

        log.debug("Result of WSFT request: " + result);

The data I am retrieving from the GIS database is encoded in UTF-8, and all the features I would expect to find are found. However, any special characters are not being printed properly by my debug statements, or being displayed properly in the front end of my application (a Spring MVC web app).

I know the values are being stored correctly in my GIS database as I can see them via SQL client and they are printed as I would expect. I can also see the names of Roads etc which use special characters are being printed properly on my map layers which suggests the GeoServer is configured correctly.


Solution

  • Instead of passing in a BasicResponseHandler, use the HttpClient.execute(HttpUriRequest) method which returns a HttpResponse and then use EntityUtils.toString(HttpEntity, "UTF-8"), pseudo:

    HttpResponse r = httpClient.execute(httPost)
    String utf8encodedEntity = EntityUtils.toString(r.getEntity(), "UTF-8");