我想关闭Jersey请求或某些参数的URL编码。
我说的服务器也需要example.com/path?query=foo:bar
的格式
随着Jackson WebTarget,
final WebTarget target = ClientBuilder.newClient()
.target(url)
.queryParam("query", "{queryVal}")
.resolveTemplate("queryVal", "foo:bar");
遗憾的是,这会产生服务器不接受的example.com/path?query=foo bar
。
我已经搜索了很多这个,唯一有希望的途径似乎与javax.ws.rs.core.Configuration
有关,但我还没有达到目的。
我明白了:解决方案是使用Advanced Features of the Client API中记录的请求过滤器
final WebTarget target = ClientBuilder.newClient()
.target(url)
.queryParam("query", "{queryVal}")
.resolveTemplate("queryVal", "foo:bar")
.register(Filter.class)
然后我们有
@Provider
public class Filter implements ClientRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(Filter.class);
@Override
public void filter(ClientRequestContext ctx) throws IOException {
try {
logger.debug("Before: {}", ctx.getUri());
//this is gonna get ugly
ctx.setUri(new URI(
URLDecoder.decode(ctx.getUri().toString(), StandardCharsets.UTF_8.toString())));
logger.debug("After: {}", ctx.getUri());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
输出:
com.example.client.Filter : Before: http://example.com/path?query=foo%3Abar
com.example.client.Filter : After: http://example.com/path?query=foo:bar