Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jackson with a web proxy

Tags:

java

jackson

I'm writing class to read from a JSON web service using Jackson. Previously, when reading from a web service I've used a custom web browser class to be able to set certain connection information, such as proxy host/port/username/password, etc as well as read and connection timeout values.

Is there a way to do this in Jackson natively? E.g. by setting the proxy parameters in a configuration?

Or should I revert back to getting the API response as a string and then using Jackson to parse it?

FYI, this is the (simplified) code that I am using.

URL configUrl = new URL("http://my.webservice.com/api");
ConfigClass localConfig = mapper.readValue(configUrl, ConfigClass.class);
like image 985
Omertron Avatar asked Oct 27 '25 20:10

Omertron


2 Answers

I would retrieve the api response as a Reader (or InputStream), and then use Jackson to parse that. Jackson just calls configUrl.openStream() under the hood, and there's no reason not to do that yourself.

like image 125
Sean Reilly Avatar answered Oct 29 '25 10:10

Sean Reilly


I think you should do the latter, proxy support hasn't been added to Jackson. Plus it's pretty simple, using the Proxy class.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
URL url = new URL("URL");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
like image 24
Mob Avatar answered Oct 29 '25 11:10

Mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!