I am trying to consume a RESTFUL web service using Java(HttpURLConnection and InputStream).I am able to print the response using BufferedReader, but it returns a response header as well and the format is causing issues to convert it to a Java POJO.
I tried using a URLConnection and then retrieving the input stream and passing it to the ObjectMapping(provided by Jackson)
final URL url = new URL("url");
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("GET");
final ObjectMapper objectMapper = new ObjectMapper();
MyData myData = objectMapper.readValue(uc.getInputStream(), MyData.class);
Error Message : "No content to map due to end-of-input\n"
In your code you don't show where you actually read the data and where you declared and filled your output variable. As code is now it seems to be the incorrect reading from your rest service. But instead of writing your own code to read fro rest url I would suggest to use the 3d party library that does it for you. Here is few suggestions: Apache Http Client, OK Http client and finally my favorite - MgntUtils Http Client (library written and maintained by me) Here is the HttpClient javadoc, Here is the link to The latest Maven artifacts for MgntUtils library and here MgntUtils Github link that contains library itself with sources and javadoc. Choose some Http Client and read the content using that client and then you can use the content.
Related
I have requirement to post a soap xml to a POST endpoint. But before posting, I need to do a health check of that url or find the status of that url. I tried to do an HttpURLConnection 'GET' method, but the url does not support 'GET'. Please help!
As you said yourself in the question that method is POST and not GET. So sending a GET request is irrelevant (regardless of whether it works or not). You can send a POST request using HttpURLConnection. But you will have to read and learn how to properly do it. The lazy way is to use a 3d party HttpClient. Here are a few options:
Apache HttpClient - a very widely used library
OK HttpClient - Open Source library
And my favorite (Open Source library written by me) MgntUtils library
With MgntUtils library your code could be as simple as
private static void testHttpClient() {
HttpClient client = new HttpClient();
client.setContentType("application/json; charset=utf-8");
client.setConnectionUrl("http://www.your.url.com/");
String content = null;
try {
content = client.sendHttpRequest(HttpMethod.POST);
} catch (IOException e) {
content = TextUtils.getStacktrace(e, false);
}
System.out.println(content);
}
Here is Javadoc for MgntUtils HTTPClient class. The library itself could be found here as Maven artifacts or on Git (including sources and JavaDoc). An article about the library (although it doesn't describe HttpClient feature) could be found here
I am loading model in apache jena using function FileManager.get().loadModel(url).And I also know that there may be some URLs in HTTP Response Link Header .I want to load model also from the links(URLs) in link header.How to do that ? Is there any inbuilt fuctionality to get access to header and process link header in Response header?
FileManager.get().loadModel(url) packages up reading a URL and parsing the results into a model. It is packing up a common thing to do; it is not claiming to be comprehensive. It is quite an old interface.
If you wanted detailed control over the HTTP handling, see if HttpOp (a lower level) mechanism helps, otherwise do the handling in the application and hand the input stream for the response directly to the parser.
You may also find it useful to look at the code in RDFDataMgr.process for help with content negotiation.
I don't think that this is supported by Jena. I don't see any reason in doing so. The HTTP request is done to get the data and maybe also to get the response type. If you want to get the URLs in some header fields, why not simply use plain old Java:
URL url = new URL("http://your_ontology.owl");
URLConnection conn = url.openConnection();
Map<String, List<String>> map = conn.getHeaderFields();
I need to be able to use a password protected proxy and be able to read json information returned from a url.
I do not want to declare proxies at the system level; I would like to have multiple proxies being used in the same application.
What is the best way to do this?
I once faces the same problem. Unfortunately, JSoup is not a good choice for this. I ended up using the apache http client, which works nicely with proxies.
Here is the proxy-relevant part of my http-client configuration:
String ipStr = "the.proxy.ip.string";
int port = 8080;
String proxyLogin = "your login name";
String proxyPassword = "your password";
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(ipStr, port),
new UsernamePasswordCredentials(proxyLogin, proxyPassword));
HttpHost httpHost = new HttpHost(ipStr, port, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
You can use the http-client to get the website or JSON response from the net. If the content is HTML, you can use JSoup as parser with the returned input. If you get JSON back, then you probably want to use a JSON parser like json-simple (but there are many other very useful JSON libraries out there!)
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);
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.
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();
Is there any way I can access RESTful webservice using a servlet. I dont want to use Jersey client?
EDIT: How can I pass object in the url and make sure that marshalling/unmarshalling is done properly?
You can use the commons-httpclient library (http://hc.apache.org/httpcomponents-client-ga/) to make a request to your REST service, and gson (http://code.google.com/p/google-gson/) to serialize/deserialize java objects to JSON
You could code your own HTTP requests e.g. using HTTPURLConnection. Here you can set the request method and change the URL or/and body as appropiate e.g.
URL url = new URL("http://www.example.com/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// etc
This way you're just using the standard java.net API.
You don't need to use jersey client, it's just a URL, you can use:
new URL("http://locationofservice").openConnection();