Showing posts with label HowTo. Show all posts
Showing posts with label HowTo. Show all posts

Tuesday, May 19, 2015

How to embed Gist source code into Blogger?

No comments:
I hope you all know about "Gist" If you don't know the definition already, here you go...

"Gist is a simple way of sharing source code snippets or samples with others" - Taken from definition.

Today we are going to see how easy to embed Gist code with our blogs. You would have faced such a pain, when you write some technical article with lots of source code references(Could be any language :() Formatting the source code will take much longer time, than your whole content of an article.

Here I am going to explain, how easy it is to embed the external source code into our blog posting without much effort. Looks simple...

Yes it is. Here you go...

<div class="gistLoad" data-id="GIST_ID" id="gist-GIST_ID"></div>
<script src="https://gist.github.com/zarub2k/GIST_ID.js"></script>
<script src='https://raw.github.com/moski/gist-Blogger/master/public/gistLoader.js' type='text/javascript'></script>

GIST_ID: This is the actual Gist id which can be taken after you create any Gist. This has to be replaced with the original id.

The actual Gist source code is being referred by the second line. This line has to be replaced with the original Gist source link.

Finally this is how the source code will be rendered in your blog post. Enjoy sharing lot of source codes :)

Share the post and give your feedback and comments if any...

Read More

Sunday, September 21, 2014

How to write REST client with proxy configurations?

3 comments:

Proxy?

Most of the enterprises have the proxy settings in-order to hide the actual endpoint from the client. Client will interact with the proxy, but the proxy will forward the request to the actual endpoint and return the response back to the client.

So what?

Till now we have seen code, how to communicate directly to the REST end-point. So the approach is straight forward. We don't need to have any special handling for the communication. Where-as if the REST service is hosted behind the proxy how to communicate?

Still thinking how? Don't worry it is very easy with Jersey client API.

client = ClientBuilder.newClient();
client.property(ClientProperties.PROXY_URI, "<proxy_host>:<proxy_port");
After created the client object, we have to add the property with specific with the proxy url configuration into the client.

In some cases, our network proxy has been restricted with user name and password. In such a situation we need to set the user name and password configuration into the client object as below,

client.property(ClientProperties.PROXY_USERNAME, "<username>");
client.property(ClientProperties.PROXY_PASSWORD, "<password>");
This is how we have to communicate with the REST end-point if our services are available behind proxy.

I hope this has clarified your doubts on REST client with proxy configuration. If you have any comments or questions please add your comments below...

Read More

Friday, September 19, 2014

How to write POST method in RESTful Java using Jersey?

7 comments:

Introduction:

In our previous articles, we have seen how to write basic REST services using Jersey framework. And also we saw how to write REST client using Jersey client API. In this article we are going to see, how to write POST method and how to consume the API.

Implementation:

POST method is a special method and it being used interchangeably based on the situation. Meaning some will use POST method for updating an object and some people are using POST method for creating an object. It is up to the user who can decide the situation based on their need.

As you guessed so, the POST method is annotated with @POST annotation. In the below example there are 2 variables are passed from the client.
title
author
Both these parameters are annotated with @FormParam annotation.

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String createBook(@FormParam(value = "title") String title, @FormParam(value = "author") String author) {
Book book = new Book(title, author);
Gson gson = new Gson(); //Gson is used to simplify the JSON generation process
return gson.toJson(book); //Just send back the JSON to the client. But user can do anything...
}

Client code:

JAX-RS and Jersey have provided lot of useful API's to write REST client code.

//Setting the post method url to the client
WebTarget webTarget = client.target("http://localhost:8080/restfullab/api").path("book");

//Add key-value pair into the form object
Form form = new Form();
form.param("title", "RESTful Java with JAX-RS 2.0 ");
form.param("author", "Bill Burke");

//Send the form object along with the post call
Response response = webTarget.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));
System.out.println("Respose code: " +  response.getStatus());
System.out.println("Respose value: " + response.readEntity(String.class));

The parameters will be passed through 'Form' values as key-value pair. 'Key' should match with the @FormParam annotation value.

I hope you have enjoyed reading this post. Please give your valuable feedback or comments if you have any...

We will meet again in few days in another article. Until then enjoy hacking RESTful Java and Jersey framework! 

Source references:

Working source code is available under the following location in Github. Fork and enjoy...
https://github.com/LiquidLab/restfullab/blob/master/src/com/liquidlab/restfullab/resources/BookResource.java
https://github.com/LiquidLab/restfullab/blob/master/test/com/liquidlab/restfullab/resources/test/BookResourceTest.java

Refer Jersey Javadoc for further details...

Read More

Tuesday, September 9, 2014

How to write REST service client with JAX-RS API?

No comments:

Introduction:

The purpose of the article is how to write REST service client code JAX-RS api. I hope you have understood the basic concepts of RESTful web services and how to write services with Jersey framework. Now we will write some code to consume the REST service using the JAX-RS client API.

Possible ways of REST client:

There are many possible ways through which we can communicate with the available rest services. Here our main focus is to communicate with rest services using JAX-RS code. The other possible ways are the following,

  • CURL
  • Advanced REST client (browser plugin)
  • Post Man (browser plugin)
In one of the previous example, I have explained how to write rest services. Based on the above article I assume, there is a service running in Tomcat in the following url,
http://localhost:8080/restfullab/api
And we have defined one resource with GET method in "hello"

So the complete url to access the rest end-point is http://localhost:8080/restfullab/api/hello

Client code:

Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://localhost:8080/restfullab/api").path("hello");

Response response = webTarget.request(MediaType.TEXT_PLAIN).get();
System.out.println(response.getStatus()); //Prints the response code. 200 if OK
System.out.println(response.readEntity(String.class)); // Prints the response text


I hope this article have given an overview on how to call REST service using JAX-RS api. In the next article we will see how to call other types of methods like POST, PUT and DELETE.

Give your valuable feedback if you have any... See you next time...

Read More