Mime type issue with Geoserver and Spring
Trying to code a simple client for OGC WFS implemented with GeoServer I got the exception:
Invalid mime type "text/xml; subtype=gml/2.1.2": Invalid token character '/' in token "gml/2.1.2"
The source of the problem is Content-Type header value - "text/xml; subtype=gml/2.1.2", which makes Spring insane when getContentType() is executed.
How to deal with that? Create the following custom RestTemplate that replaces "text/xml; subtype=gml/2.1.2" with "text/xml":
class WfsRestTemplate extends RestTemplate {
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback callback, final ResponseExtractor<T> responseExtractor) throws RestClientException {
return super.doExecute(url, method, callback, response -> {
response.getHeaders().setContentType(MediaType.TEXT_XML);
return responseExtractor.extractData(response);
});
}
}
Using this piece of code you make an assumption that the particular service is always returning something that is XML. What if not? While response.getHeaders().getContentType() gives you same exception we are trying to get rid of, it is still possible to check the header using the general method:
String contentType = response.getHeaders().getFirst("Content-Type");
if (contentType.startsWith("text/xml")) response.getHeaders().setContentType(MediaType.TEXT_XML);
Invalid mime type "text/xml; subtype=gml/2.1.2": Invalid token character '/' in token "gml/2.1.2"
The source of the problem is Content-Type header value - "text/xml; subtype=gml/2.1.2", which makes Spring insane when getContentType() is executed.
How to deal with that? Create the following custom RestTemplate that replaces "text/xml; subtype=gml/2.1.2" with "text/xml":
class WfsRestTemplate extends RestTemplate {
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback callback, final ResponseExtractor<T> responseExtractor) throws RestClientException {
return super.doExecute(url, method, callback, response -> {
response.getHeaders().setContentType(MediaType.TEXT_XML);
return responseExtractor.extractData(response);
});
}
}
Using this piece of code you make an assumption that the particular service is always returning something that is XML. What if not? While response.getHeaders().getContentType() gives you same exception we are trying to get rid of, it is still possible to check the header using the general method:
String contentType = response.getHeaders().getFirst("Content-Type");
if (contentType.startsWith("text/xml")) response.getHeaders().setContentType(MediaType.TEXT_XML);
Комментарии
Отправить комментарий