Spring:如何避免WFS请求中的InvalidMimeTypeException(“text / xml; subtype = gml / 2.1.2”)

时间:2015-07-16 07:42:58

标签: java spring mime-types gml media-type

我从WFS服务获得响应,并返回以下错误。 似乎不支持此MediaType。

org.springframework.http.InvalidMediaTypeException: Invalid mime type "text/xml; subtype=gml/2.1.2": Invalid token character '/' in token "gml/2.1.2"
at org.springframework.http.MediaType.parseMediaType(MediaType.java:370)
at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:550)
at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:116)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:87)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:809)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:793)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:572)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:357)

...

Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "text/xml; subtype=gml/2.1.2": Invalid token character '/' in token "gml/2.1.2"
at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:250)
at org.springframework.http.MediaType.parseMediaType(MediaType.java:367)

我见过How to overcome Invalid token character caused by Content-Type=[text/xml; subtype=gml/2.1.2] in Spring HttpMessageConverterExtractor?,但我无法使这个解决方案适应我的方法:我不是基于XML,也不是基于Spring集成的解决方案。

我的代码片段:

RestTemplate template = new RestTemplate();
String url = "http://some.geo.server/wfs?";
String query = buildWFSSpatialQuery(geometry, type);
ResponseEntity<String> entity = template.postForEntity(url, query, String.class);

其中query类似于

<wfs:GetFeature service="WFS" version="1.0.0" maxFeatures="1" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><wfs:Query ....

错误可以很容易地重现:

org.springframework.http.MediaType.parseMediaType("text/xml; subtype=gml/2.1.2");

1 个答案:

答案 0 :(得分:0)

好的,有办法做到这一点。你要做的就是用......

setDefaultResponseContentType("text/xml; subtype=\"gml/2.1.2\"")