Dependecny Injection using Grizzly and Jersey

Statement: Implementation of Dependency Injection using Grizzly and Jersey

Please follow the below steps to do the same –

  • Create a class called Hk2Feature which implements Feature.

package com.sample.di;





public class Hk2Feature implements Feature {

  public boolean configure(FeatureContext context) {

    context.register(new MyAppBinder());

    return true;



  • Create a class called MyAppBinder which extends AbstractBinder and you need to register all the services here like below –

package com.sample.di;

import org.glassfish.hk2.utilities.binding.AbstractBinder;

public class MyAppBinder extends AbstractBinder {


  protected void configure() {




  • Now, it’s time to write your own services and inject all the required services in your appropriate controllers like below code –

package com.sample.di;

public class MainService {

  public String testService(String name) {

    return “Hi” + name + “..Testing Dependency Injection using Grizlly Jersey “;



package com.sample.di;

import javax.inject.Inject;

public class MainController {

public MainService mainService;

public String get(@QueryParam(“name”) String name) {
return mainService.testService(name);

public String ping() {
return “OK”;

Now hit the url http://localhost:8080/main?name=Tanuj and you will get your result. This is how you can achieve dependency injection in Grizzly Jersey application. Find the detailed implementation of the above skeleton in my repo. Happy Coding 🙂

Dockerize Java RESTful Application

Please follow the below steps to dockerize your java RESTful application –

Prerequisites : Please insure that Docker, java and mvn is installed on your machine.

  1. Create Dockerfile : Go to the root of the application where pom.xml is contained. Below is the content of my Dockerfile –

cd yourProjectFolder -> vi Dockerfile

#Fetch the base Jav8 image

FROM java:8

#Expose the local application port


#Place the jar file to the docker location

ADD /target/lookupService-1.0-SNAPSHOT.jar lookupService-1.0-SNAPSHOT.jar

#Place the config file as a part of application

ADD /src/main/java/com/test/config/

#execute the application

ENTRYPOINT [“java”,”-jar”,”yourService-1.0-SNAPSHOT.jar”]

  1.  Build Docker Image :

docker build -f Dockerfile -t yourservice .

  1. Run the Docker Image :

docker run -p 8088:8088 -tyourservice

Option -p publishes or maps host system port 18080 to container port 8080.

Note* Please don’t forget to change your base uri that is localhost:8088 to network interface created inside the container listen to all available address.

Few important commands of docker to know :

  1. To find the Id of your running container –

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

77558fabec6c       yourservice       “java -jar lookupS…”   38 minutes ago      Up 38 minutes>8088/tcp   affectionate_brahmagupta

  1. To kill the process inside the docker using container is –

docker container kill 77558fabec6c(Container Id)

Now you can test your RESt call on port using any of the REST client . Hope it helps 🙂

Expose a local web server to the internet

ngrok allows you to expose a web server running on your local machine to the internet. Just tell ngrok what port your web server is listening on.

If you don’t know what port your web server is listening on, it’s probably port 80, the default for HTTP.

Example: Expose a web server on port 9876 of your local machine to the internet
ngrok http 9876

When you start ngrok, it will display a UI in your terminal with the public URL of your tunnel and other status and metrics information about connections made over your tunnel.

The ngrok console UI
ngrok by @inconshreveable
Tunnel Status                 online
Version                       2.0/2.0
Web Interface       
Forwarding           -> localhost:9876
Forwarding           -> localhost:9876
Connnections                  ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

Hope it helps. For more details, you can refer

Way to redirect the url from HTML page getting in the response of REST call through AJAX

Problem Statement – I have one CDN server (mylocalmachinedomain:8022) hosted through XAMPP (Apache) on my local machine. In parallel to it, I have written the micro-service (  to expose it’s REST end point hosted on tomcat server on my local machine. Now First I have to make a REST call (expose end point written in my micro-service) from the HTML page through AJAX. Secondly while exposing the REST call, it would redirect to some other page. So that I need to handle in the HTML page through AJAX. There are 2 ways to do the same –

My micro-service end point java code-



    @ApiOperation(value = “Displays a message”, notes = “The message is read from the properties”, response = Test.class )

    @ApiResponses(value = { @ApiResponse(code = 500, message = “Something went wrong”, response=Test.class) })


    public Response authCall() throws Exception {“This is a log to check oAuth api”);

        //HttpsURLConnection conn = null;

        String url = “;

        URI uri=new URI(url);

        return Response.status(Status.TEMPORARY_REDIRECT).location(uri).header(“Content-Type”, “application/json;charset=UTF-8”).build();


 1st way –



  type: “GET”,

  url: ““,

  success: function(data){






2nd way –

var xmlhttp;

    function init() {

       // put more code here in case you are concerned about browsers that do not provide XMLHttpRequest object directly

       xmlhttp = new XMLHttpRequest();

        var url =”“;‘GET’,url,true);


        xmlhttp.onreadystatechange = function() {

               if (xmlhttp.readyState == 4) {

                  if ( xmlhttp.status == 200) {






                       alert(“Error ->” + xmlhttp.responseText);




<body onload=”init()”> 


Hope it helps 🙂

Git – Permission denied (publickey) Error while cloning the repo on Windows.

The above error comes when either you haven’t made any SSH keys on your local machine or those keys are expired. So to solve this, Please follow the below steps –

  1. Open git bash.
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and
  4. Copy the content of with no extra spaces or lines and paste it into GitHub under the Account Settings > SSH Keys. You can name as per your convenience.
  5. Now git clone or git pull should work.

In case the above files are not created, you can use the below command to do so –

cd ~/.ssh && ssh-keygen

Now follow all the above mentioned steps. Hope it will solve your problem.

How to redirect an URL using REST Java

There are multiple ways to perform redirection. Actually 3x family of HTTP plays a vital role while performing URL redirection in the below ways –

1. 301 Moved Permanently

String url =;;

URI uri=new URI(url);

return Response.status(Status.MOVED_PERMANENTLY).location(uri).build();

Now it’ll redirect you to your localhost endpoint defined in the parameter redirect_uri permanently.

2. 307 Temporary Redirect

return Response.temporaryRedirect(uri).build();


How to enable SSL/HTTPS in Java Micro-Service/Spring-Boot application

  1. Generate SSL certificate –

keytool -genkey -alias <alias> -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

  1. Add the below lines in your file –

server.port: 8443

server.ssl.key-store: classpath : keystore.p12

server.ssl.key-store-password: <keystore password>

server.ssl.keyStoreType: PKCS12

server.ssl.keyAlias: <alias>

3. Restart your java application. Now you’ll be able to access your application over HTTPS on https://localhost:8443 instead of http://localhost:8080.